boost::program_options :当我自己的选项类型属于命名空间时,如何声明和验证它?

发布于 2024-09-10 21:35:33 字数 1174 浏览 5 评论 0原文

使用 boost::program_options,当在命名空间内声明我自己的选项类型时,我无法编译它。然而,在命名空间之外,它可以编译并正常工作:

#include <boost/program_options.hpp>
using namespace boost;
using namespace boost::program_options;

struct my_type1 {
    my_type1(int nn) : n(nn) {}
    int n;
};
namespace nm  {
    struct my_type2 {
        my_type2(int nn) : n(nn) {}
        int n;
    };
}

void validate(boost::any& v,
              const std::vector<std::string>& values,
              my_type1*, int)  {
    const std::string& s = validators::get_single_string(values);
    v = any(my_type1(lexical_cast<int>(s)));
}
void validate(boost::any& v,
              const std::vector<std::string>& values,
              nm::my_type2*, int)  {
    const std::string& s = validators::get_single_string(values);
    v = any(nm::my_type2(lexical_cast<int>(s)));
}

int main()  {
    options_description desc("options");
    desc.add_options()
        ("m1", value<my_type1>()    , "")
        ("m2", value<nm::my_type2>(), "")
    ;
    return 0;
}

在 main() 中,选项“m1”的声明可以编译,但“m2”则不能... 缺什么 ? 我正在使用 boost_1_43_0 和 gcc 版本 4.4.4。

Using boost::program_options, I can not get my own option type to compile when it is declared inside a namespace. However outside of the namespace it compiles and works fine :

#include <boost/program_options.hpp>
using namespace boost;
using namespace boost::program_options;

struct my_type1 {
    my_type1(int nn) : n(nn) {}
    int n;
};
namespace nm  {
    struct my_type2 {
        my_type2(int nn) : n(nn) {}
        int n;
    };
}

void validate(boost::any& v,
              const std::vector<std::string>& values,
              my_type1*, int)  {
    const std::string& s = validators::get_single_string(values);
    v = any(my_type1(lexical_cast<int>(s)));
}
void validate(boost::any& v,
              const std::vector<std::string>& values,
              nm::my_type2*, int)  {
    const std::string& s = validators::get_single_string(values);
    v = any(nm::my_type2(lexical_cast<int>(s)));
}

int main()  {
    options_description desc("options");
    desc.add_options()
        ("m1", value<my_type1>()    , "")
        ("m2", value<nm::my_type2>(), "")
    ;
    return 0;
}

In main(), declaration of option 'm1' compiles but 'm2' does not...
What is missing ?
I am using boost_1_43_0 with gcc version 4.4.4.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

埋情葬爱 2024-09-17 21:35:33

验证函数应该与您的结构 my_type 位于同一名称空间中,更改为:

  namespace nm  {
     void validate(boost::any& v,
                const std::vector<std::string>& values,
                  my_type2*, int)  {
      const std::string& s = validators::get_single_string(values);
      v = any(my_type2(lexical_cast<int>(s)));
    }
  }

它为我编译。

The validate function should be in the same namespace as your struct my_type, change to:

  namespace nm  {
     void validate(boost::any& v,
                const std::vector<std::string>& values,
                  my_type2*, int)  {
      const std::string& s = validators::get_single_string(values);
      v = any(my_type2(lexical_cast<int>(s)));
    }
  }

the it compiles for me.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文