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

前提是你 提交于 2019-12-06 01:38:19

问题


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>'


回答1:


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" )


来源:https://stackoverflow.com/questions/6921196/in-boostprogram-options-how-to-set-default-value-for-wstring

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!