http://www.boost.org/doc/libs/1_58_0/doc/html/program_options/tutorial.html
// Declare the supported options.
.............................................
desc.add_options()
("help", "produce help message")
("compression", po::value<int>(), "set compression level")
;
是通过运算符重载吗?
如果是,哪个运算符在这里被重载?
您可以使用简单的非 Boost 示例程序来模仿此语法吗?
desc.add_options()
返回一个重载的对象operator()
。这意味着该对象可以像函数一样被调用。
更具体,options_descriptions::add_options()
返回一个options_description_easy_init
目的。这个对象有一个operator()
,它返回对的引用*this
:任何调用operator()
返回对的引用options_description_easy_init
对象本身,因此可以再次调用它。
您可以找到两者的源代码options_descriptions
and options_description_easy_init
here.
要自己复制这个,你可以这样做:
#include <iostream>
class callable {
public:
class callable &operator()(const std::string &s) {
std::cout << s << std::endl;
return *this;
}
};
callable make_printer() {
return callable();
}
int main() {
make_printer()("Hello, World!")("Also prints a second line");
return 0;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)