在Boost::Program_Options中,如何设置wstring的默认值

In Boost::Program_Options, how to set default value for wstring?

本文关键字:设置 wstring 默认值 何设置 Program Boost Options      更新时间:2023-10-16

下面的代码不起作用:

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<CharT,Base,Traits>' 的成员

长篇大论的解释:这是因为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" )