我喜欢直奔源头。所以,给你:https://github.com/boostorg/program_options/blob/develop/include/boost/program_options/variables_map.hpp#L52:
/** Runs all 'notify' function for options in 'm'. */
BOOST_PROGRAM_OPTIONS_DECL void notify(variables_map& m);
因此,它运行variables_map、m 的所有notify() 函数,它是std::map<std::string, variable_value> 的包装器,并包含从命令选项(如std::strings)到variable_value 的映射s(任何类型)。
现在this answer by @Conspicuous Compiler 有更多上下文,所以去阅读吧。除了源代码之外,查看他的答案对我有帮助。
另外(正如 OP 已经知道的,但这是给其他人的),这里有一个 关于如何使用 <boost/program_options.hpp> 模块的介绍性 Boost 教程:https://www.boost.org/doc/libs/1_75_0/doc/html/program_options/tutorial.html. 这真的有点“挥手。”他们不希望您知道这些特定功能的真正作用。他们只想让你知道如何遵循模式和使用库,正如他们暗示的那样:
接下来,对store、parse_command_line 和notify 函数的调用导致vm 包含在命令行中找到的所有选项。
(参考下面示例中的这段代码):
po::store(po::parse_command_line(ac, av, desc), vm);
po::notify(vm);
当您调用 add_options() 函数时,他们正在 C++ 中做一些非常漂亮的黑色魔法 vudu。
这是他们的基本教程示例的完整上下文:
example/first.cpp:
// Copyright Vladimir Prus 2002-2004.
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt
// or copy at http://www.boost.org/LICENSE_1_0.txt)
/* The simplest usage of the library.
*/
#include <boost/program_options.hpp>
namespace po = boost::program_options;
#include <iostream>
#include <iterator>
using namespace std;
int main(int ac, char* av[])
{
try {
po::options_description desc("Allowed options");
desc.add_options()
("help", "produce help message")
("compression", po::value<double>(), "set compression level")
;
po::variables_map vm;
po::store(po::parse_command_line(ac, av, desc), vm);
po::notify(vm);
if (vm.count("help")) {
cout << desc << "\n";
return 0;
}
if (vm.count("compression")) {
cout << "Compression level was set to "
<< vm["compression"].as<double>() << ".\n";
} else {
cout << "Compression level was not set.\n";
}
}
catch(exception& e) {
cerr << "error: " << e.what() << "\n";
return 1;
}
catch(...) {
cerr << "Exception of unknown type!\n";
}
return 0;
}