在Boost :: Program_Options中,如何设置wstring的默认值? [英] In Boost::Program_Options, how to set default value for wstring?

查看:244
本文介绍了在Boost :: Program_Options中,如何设置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:'setg':不是'boost :: detail :: lexical_stream_limited_src< CharT,Base,Traits>'

blockquote>

解决方案

长时间的解释:这是因为底层的 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 in program_options tries to do a lexical cast from wchar to char in setting the m_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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆