Boost logo

Boost :

Subject: [boost] [program options] Support for storage to 'optional' objects
From: Chris Glover (c.d.glover_at_[hidden])
Date: 2012-10-15 02:27:36


Hi,

Recently when using the Boost Program options library, I was surprised to
find that I was unable to pass a boost.optional object to store supplied
options in. For example;

namespace bpo = boost::program_options;
boost::optional<std::string> opt_str;
bpo::options_description desc("Allowed options");
desc.add_options()
    ("help", "produce help message")
    ("opt", bpo::value(&opt_str), "Something optional") // Store value
of 'opt' in 'opt_str'
;

What I expected to happen would be that 'opt_str' would be valid if 'opt'
is supplied by the user, and not valid otherwise.

However, the above code causes a compile error deep within lexical_cast
because, of course, performing such an operation on a boost.optional
doesn't make sense. The attached patch to Program Options rectifies the
issue by filtering out optional objects within before calling lexical_cast,
but to be honest, I'm not sure if this is the correct approach or if this
is even a desirable feature. I like the idea of setting an optional object
this way instead of testing the variables_map later on, but perhaps there's
something I am missing or a better way to accomplish the same thing.

Please have a look and let me know your thoughts.

Thanks,

-- chris




Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk