gpt4 book ai didi

c++ - boost::program_options::notify() 有什么用?

转载 作者:IT老高 更新时间:2023-10-28 12:41:51 25 4
gpt4 key购买 nike

这个问题是关于 C++ Boost program_options 库的。

所有教程都非常清楚,我应该在我完成的变量映射上调用 notify(),但我不确定这实际上对我做了什么。注释掉似乎没有任何效果,文档也没有详细说明:

http://www.boost.org/doc/libs/1_47_0/doc/html/boost/program_options/notify.html

其他消息来源表明它运行“用户定义”功能。如果是这样,这些函数是如何注册的,它们是做什么的?他们会抛出异常吗?

最佳答案

notify()member function of value_semantic .它是一个钩子(Hook),一旦确定了选项的最终值,该选项应该采取的任何操作都可以自动完成并封装在它自己的函数中。这可以防止代码具有一个作用于每个选项的长函数。随着可能选项的增加,这种程序代码可能会变得笨拙。

你可以看到an example of setting a notify function in the Boost manual :

options_description desc;
desc.add_options()
("compression", value<int>()->default_value(10), "compression level")
("email", value< vector<string> >()
->composing()->notifier(&your_function), "email")
;

These declarations specify that default value of the first option is10, that the second option can appear several times and all instancesshould be merged, and that after parsing is done, the library willcall function &your_function, passing the value of the "email" optionas argument.

关于c++ - boost::program_options::notify() 有什么用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7125666/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com