Search code examples
c++boostboost-program-options

Strange boost program_options issue using boost vector and string


Using Boost.Program_options, I have code along the lines of as follows:

namespace po = boost::program_options;
namespace ct = boost::container;

And in a particular function:

Options opts;
ct::string fname;
ct::vector<ct::string> others;

{   po::options_description general("General");
    general.add_options()
        ("version,v", "Print version information")
        ("help,h", "Show help message")
        ("verbosity,V", po::value(&opts.verbosity)->default_value(0)->value_name("level"), "Runtime noise")
        ("main-is,m", po::value(&fname)->default_value("Main.pbc")->value_name("fname"), "Program filename")
        /* error */ ("sideload,l", po::value<ct::vector<ct::string> >(&others)->multitoken(), "Other modules")
        ;

    po::options_description performance("Performance");
    performance.add_options()
        // ... code removed here ...
        ;

    po::positional_options_description modules;
    modules.add("main-is", 1).add("sideload", -1);

    po::options_description cmdline;
    cmdline.add(general).add(performance);

    po::variables_map vm;
    try {
        po::store(po::command_line_parser(argc, argv).options(cmdline).positional(modules).run(), vm);
        po::notify(vm);
    }
    catch (const std::exception &e) {
        std::cout << e.what() << std::endl;
        return EXIT_FAILURE;
    }

    if (vm.count("help") || argc == 1) {
        version();
        std::cout << std::endl << "USAGE: " << argv[0] << " [options] MAIN.pbc [sideload modules ...]" << std::endl;
        std::cout << cmdline << std::endl;
        return EXIT_SUCCESS;
    }

    if (vm.count("version")) {
        version();
        return EXIT_SUCCESS;
    }
}

I get an error which looks like this:

In file included from /Users/kvanb/git/panthera/panthera/src/main.cpp:7:
In file included from /usr/local/include/boost/program_options/options_description.hpp:13:
In file included from /usr/local/include/boost/program_options/value_semantic.hpp:14:
/usr/local/include/boost/lexical_cast.hpp:388:13: error: static_assert failed "Target type is
      neither std::istream`able nor std::wistream`able"
  ...BOOST_STATIC_ASSERT_MSG((result_t::value || boost::has_right_shift<std::basic_istream<wchar_t>, T >::value), 
     ^                       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

If I comment out the line I've commented /* error */ above, it goes away.

I've tried using std::vector and std::string instead, but the error remains the same.

Ideas?


Solution

  • Porgram Options apparently doesn't support Boost Container's boost::container::vector type for multi-value options.

    You can use std::vector<ct::string>, or look for which trait to specialize to support ct::vector as well

    E.g. in boost/program_options/detail/value_semantic.hpp you'd need to add overloads for boost::container::vector:

    /** Validates sequences. Allows multiple values per option occurrence
        and multiple occurrences. */
    template<class T, class charT>
    void validate(boost::any& v, 
                  const std::vector<std::basic_string<charT> >& s, 
                  boost::conatainer::vector<T>*, // HERE!
                  int)
    

    Honestly, as you can see they didn't even support std::vector with custom allocators. I think the devs won't be eager to implement this; the implementation is complicated enough as it is (with the overloads being tweaked to allow for compilers that don't do partial ordering of template instantiations correctly).