【问题标题】:Strange boost program_options issue using boost vector and string使用提升向量和字符串的奇怪提升程序选项问题
【发布时间】:2014-06-16 00:42:50
【问题描述】:

使用 Boost.Program_options,我的代码如下:

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

在特定的功能中:

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;
    }
}

我收到一个如下所示的错误:

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), 
     ^                       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

如果我注释掉我在上面注释过/* error */ 的那一行,它就会消失。

我尝试改用std::vectorstd::string,但错误依旧。

想法?

【问题讨论】:

  • 有趣。与std::vector&lt;std::string&gt; 配合使用效果很好。
  • 我会再试一次,但我敢肯定它刚才没有用。
  • 嗯——你说得对。那很奇怪。对不起。
  • 我很好奇这个错误,虽然我没有阅读完整的信息。 std::vectorboost::container::vector 都不是流式传输的,std::stringboost::container::string 都是流式传输的。
  • 确实很好奇。

标签: c++ boost boost-program-options


【解决方案1】:

Porgram Options 显然不支持 Boost Container 的 boost::container::vector 类型的多值选项。

您可以使用std::vector&lt;ct::string&gt;,或者也可以查找要专门支持ct::vector 的特征

例如在boost/program_options/detail/value_semantic.hpp 中,您需要为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)

老实说,正如您所见,他们甚至不支持 std::vector 使用自定义分配器。我认为开发人员不会急于实现这一点。实现本身就足够复杂了(对重载进行了调整以允许编译器不能正确地对模板实例进行部分排序)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-07-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-12
    相关资源
    最近更新 更多