【问题标题】:In Boost::Program_Options, how to set default value for wstring?在 Boost::Program_Options 中,如何设置 wstring 的默认值?
【发布时间】:2011-08-03 03:03:00
【问题描述】:

我下面的代码不起作用:

wstring config_file;
// Declare a group of options that will be 
// allowed only on command line
po::options_description generic("Generic options");
generic.add_options()
    ("help,h", "produce help message")
    ("config,c", po::wvalue<wstring>(&config_file)->default_value(L"DXDrv.cfg"), "name of a file of a configuration.")
    ;

编译失败并出现错误:

d:\repo\a4x_ext\minidxdriver\testapp\configparser\boost\lexical_cast.hpp(1096):错误 C2039:'setg':不是'boost::detail::lexical_stream_limited_src&lt;CharT,Base,Traits&gt;' 的成员

【问题讨论】:

  • 哪个部分不起作用?你期望得到什么?现在还不清楚是什么问题。
  • 我用错误消息更新了我的帖子。你能帮我看看一些工作的例子吗?非常感谢您的评论!
  • 是否有来自编译错误的模板实例化回溯?
  • 我为 Program_options 的作者找到了一些帖子。帖子说库不支持wstring的默认值。

标签: c++ boost boost-program-options


【解决方案1】:

冗长的解释:这是因为program_options 中的底层typed_value 类型试图在设置m_default_value_as_text 私有成员时进行从wcharchar 的词法转换。无论出于何种原因,basic_string 类型都没有创建正确模板类型所需的功能。

幸运的是,typed_value 类对 default_value 和implicit_value 有第二个覆盖,它提供了值的字符串表示。这绕过了 lexical_cast,从而解决了问题。类似的东西:

     tvalue< tstring >()->default_value( _T( "output.png" ), "output.png" )

【讨论】:

  • 为什么 boost program_options 需要将默认值转换为字符串?谁需要它?
  • 我刚刚意识到答案。 program_options 需要在打印帮助信息时显示该值。
  • 优秀的帖子!我和 Syncopated 有同样的问题。知道为什么当输出值为 wvalue<:wstring> 时它需要窄字符串作为默认值吗?和帮助信息有关系吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-01-15
  • 1970-01-01
  • 1970-01-01
  • 2019-06-13
  • 1970-01-01
  • 1970-01-01
  • 2011-12-31
相关资源
最近更新 更多