在Boost :: Program_Options中,如何设置wstring的默认值? [英] In Boost::Program_Options, how to set default value for wstring?
问题描述
我的代码下面没有工作:
wstring config_file;
//声明一组选项,将
//仅在命令行允许
po :: options_description generic(Generic options);
generic.add_options()
(help,h,产生帮助消息)
(config,c,po:wvalue< wstring>(& config_file) > default_value(LDXDrv.cfg),配置文件的名称)
;
编译失败,出现错误:
d:\repo\a4x_ext\minidxdriver\testapp\configparser\boost\lexical_cast.hpp(1096):错误C2039:
blockquote>'setg'
:不是'boost :: detail :: lexical_stream_limited_src< CharT,Base,Traits>'
解决方案长时间的解释:这是因为底层的
typed_value
code> program_options 尝试从wchar
到char
执行词法转换设置m_default_value_as_text
私有成员。无论什么原因,basic_string类型没有必要的函数来创建正确的模板类型。
幸运的是,typed_value类对default_value和implicit_value有第二个覆盖,它提供了值的字符串表示。这绕过了lexical_cast,这修复了这个问题。像:
tvalue< tstring>() - > default_value(_T(output.png),output.png)
My code below did not work:
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.") ;
The compilation failed with error:
d:\repo\a4x_ext\minidxdriver\testapp\configparser\boost\lexical_cast.hpp(1096) : error C2039:
'setg'
: is not a member of'boost::detail::lexical_stream_limited_src<CharT,Base,Traits>'
解决方案Long-winded explanation: This is because the underlying
typed_value
type inprogram_options
tries to do a lexical cast fromwchar
tochar
in setting them_default_value_as_text
private member. For whatever reason, the basic_string type does not have the necessary functions to create the correct template types.Luckily, the typed_value class has a second override for default_value and implicit_value which provides a string representation of the value. This bypasses the lexical_cast, which fixes the problem. Something like:
tvalue< tstring >()->default_value( _T( "output.png" ), "output.png" )
这篇关于在Boost :: Program_Options中,如何设置wstring的默认值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!