在boost :: Program_Options,如何为wstring的设置默认值? [英] In Boost::Program_Options, how to set default value for wstring?
问题描述
我下面code没有工作:
wstring的CONFIG_FILE;
//声明一组,这将是选项
//只允许在命令行
PO :: options_description通用(通用选项);
generic.add_options()
(帮助H,生产帮助信息)
(配置,C,婆:: wvalue&所述; wstring的>(&放大器; CONFIG_FILE) - >中。一个配置的一个文件的名称DEFAULT_VALUE(LDXDrv.cfg))
;
编译失败,错误:
D:\\回购\\ a4x_ext \\ minidxdriver \\ testapp \\ configparser \\提升\\ lexical_cast.hpp(1096):错误C2039:
'SETG
:是不是成员'的boost ::详细:: lexical_stream_limited_src<图表,基地,性状及GT;
块引用>解决方案啰嗦的解释:这是因为底层
typed_value
键入program_options
试图在制定<$ C $办从WCHAR
词法投地字符
C> m_default_value_as_text 私有成员。无论出于何种原因,basic_string的类型没有必要的功能来创建正确的模板类型。幸运的是,typed_value类有DEFAULT_VALUE和implicit_value它提供了一个重新的字符串值presentation第二个覆盖。这绕过lexical_cast的,它解决了这个问题。是这样的:
tvalue&LT; tstring&GT;() - &GT; 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屋!