11

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

ildjarn
  • 62,044
  • 9
  • 127
  • 211
MQ Gu
  • 643
  • 2
  • 9
  • 19
  • Which part isn't working? What did you expected to get? Now it's not clear what the problem is. – Kirill V. Lyadvinsky Aug 03 '11 at 04:25
  • I updated my post with the error message. Could you help to show me some working example? Thanks very much for your comment! – MQ Gu Aug 03 '11 at 04:48
  • Was there a template instantiation back-trace from the compile error? – Pete Aug 03 '11 at 08:11
  • I found some post for the auther of Program_options. The post said the default value of wstring is not supported by the library. – MQ Gu Aug 04 '11 at 06:38

1 Answers1

16

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" )
Sahab Yazdani
  • 176
  • 2
  • 3
  • 1
    Why does boost program_options bother with converting the default value to a string? Who needs it? – Joshua Chia Jan 29 '13 at 09:48
  • I just realized the answer. program_options needs to show the value when printing the help message. – Joshua Chia Jan 29 '13 at 13:01
  • Excellent post! I have the same question as Syncopated. Any idea why it needs the narrow string for the default value when the output value is wvalue ? Does it have to do with the help message? – namezero Sep 06 '13 at 20:36