MQ *_* Gu 6 c++ boost boost-program-options
我的代码不起作用:
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.")
;
Run Code Online (Sandbox Code Playgroud)
编译失败,错误:
d:\ repo\a4x_ext\minidxdriver\testapp\configparser\boost\lexical_cast.hpp(1096):错误C2039 ::
'setg'不是'boost::detail::lexical_stream_limited_src<CharT,Base,Traits>'
小智 12
冗长的解释:这是因为底层typed_value类型program_options试图在设置私有成员时进行词法强制wchar转换.无论出于何种原因,basic_string类型都没有必要的函数来创建正确的模板类型.charm_default_value_as_text
幸运的是,typed_value类对default_value和implicit_value有第二个覆盖,它提供了值的字符串表示.这绕过了lexical_cast,修复了问题.就像是:
tvalue< tstring >()->default_value( _T( "output.png" ), "output.png" )
Run Code Online (Sandbox Code Playgroud)