【问题标题】:How do I specify a default value for vector<string> in boost::program_options如何在 boost::program_options 中为 vector<string> 指定默认值
【发布时间】:2012-08-10 10:11:27
【问题描述】:

我确实想在代码中的注释中为位置参数提供默认值,但编译器会抱怨。代码编译得很好。我使用 boost 1.46.1 和 g++

int main(int argc, char *argv[]) {
    namespace po = boost::program_options;

    po::positional_options_description p;
    p.add("path", -1);

    po::options_description desc("Options");
    std::vector<std::string> vec_str;
    std::string str;
    desc.add_options()
        ("foo,f", po::value< std::string >()->default_value(str), "bar")
        //("path,p", po::value< std::vector<std::string> >()->default_value(vec_str), "input files.")
        ("path,p", po::value< std::vector<std::string> >(), "input files.")
    ;

    po::variables_map vm;
    po::store(po::command_line_parser(argc, argv).options(desc).positional(p).run(), vm);
    po::notify(vm);
}

【问题讨论】:

  • 这一行编译:
    ("path,p", po::value >()->default_value(std::vector(), ""), "input files.")
    但我不知道为什么

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


【解决方案1】:

我需要给出默认值的文本表示,请参阅http://lists.boost.org/boost-users/2010/01/55054.php

即以下行有效:

 ("path,p", po::value< std::vector<std::string> > ()->default_value(std::vector<std::string>(), ""), "input files.")

我想这是帮助输出所需要的,在我的示例中可能

std::cout << desc << std::endl;

由于编译器不知道如何为vector&lt;string&gt; 重载operator&lt;&lt;(),所以它会报错。

【讨论】:

    猜你喜欢
    • 2020-08-29
    • 1970-01-01
    • 1970-01-01
    • 2019-01-15
    • 1970-01-01
    • 1970-01-01
    • 2021-12-24
    • 1970-01-01
    • 2011-11-24
    相关资源
    最近更新 更多