Fra*_*ois 3 validation boost namespaces boost-program-options
使用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;
}
Run Code Online (Sandbox Code Playgroud)
在main()中,选项'm1'的声明编译,但'm2'不会...缺少什么?我在gcc版本4.4.4中使用boost_1_43_0.
validate函数应与结构位于同一名称空间中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)));
}
}
Run Code Online (Sandbox Code Playgroud)
它为我编译.
归档时间: |
|
查看次数: |
2286 次 |
最近记录: |