升压program_options自定义验证程序不与海湾合作委员会的工作,作品有MSVC [英] Custom validator for boost program_options doesn't work with GCC, works with MSVC
问题描述
比方说,我想特别处理整数选项。根据该文件我写我自己的验证功能。考虑下面的短节目。
Let's say I would like special processing for integer options. According to the documentation I have to write my own validate function. Consider the following short program.
#include <iostream>
#include <vector>
#include <string>
#include <boost/program_options.hpp>
namespace po = boost::program_options;
namespace boost { namespace program_options {
template <class charT>
void validate(boost::any& v, const std::vector<std::basic_string<charT> >& xs, unsigned int*, int)
{
std::cout << "validate is redefined" << std::endl;
// do something else
}
}}
int main(int argc, char* argv[])
{
po::options_description cmdLineOptions;
po::variables_map vm;
unsigned int v;
const char* args[] = {"tst", "-k", "10"};
cmdLineOptions.add_options()
("key,k", po::value<unsigned int>(&v)->required())
;
po::store(po::command_line_parser(sizeof(args) / sizeof(args[0]), args).options(cmdLineOptions).run(), vm);
po::notify(vm);
std::cout << v << '\n';
return 0;
}
它完美地工作在VS 2013和输出
It perfectly works in VS 2013 and outputs
validate is redefined
10
在GCC它从未步骤验证函数内。
In GCC it never steps inside the validate function.
证明: http://coliru.stacked-crooked.com/a/fd558ebf987a4bbe
如果我尝试使用自定义类型,而不是为unsigned int,GCC会尽量尝试使用来自program_option一个验证,并最终将与一堆错误。
If I try to use a custom type instead of unsigned int, GCC would try to use a validate from program_option anyway and will end up with bunch of errors.
我在做什么错了?
推荐答案
在一个preliminary预感,可以考虑使用
On a preliminary hunch, consider using
BOOST_STRONG_TYPEDEF(unsigned int, Unsigned);
这似乎是一个坏主意来定制只是内置类型的行为。
It seems like a bad idea to customize behaviour just on built-in types.
解决方案:它有偏序做。
Solution: It has to do with partial ordering.
如果您移动过载的boost :: program_options外
命名空间就会开始工作(因为它不再与基本模板竞争)。
If you move your overload outside the boost::program_options
namespace it will start working (as it no longer competes with the base template).
<大骨节病> 住在Coliru 骨节病>
#include <iostream>
#include <vector>
#include <string>
#include <boost/any.hpp>
#include <boost/strong_typedef.hpp>
#include <boost/program_options.hpp>
BOOST_STRONG_TYPEDEF(unsigned, Unsigned)
template<class charT>
void validate(boost::any& v,
const std::vector< std::basic_string<charT> >& xs,
Unsigned* p, int)
{
std::cout << "validate is redefined" << std::endl;
// do something else
}
namespace po = boost::program_options;
int main()
{
po::options_description cmdLineOptions;
po::variables_map vm;
Unsigned v;
const char* args[] = {"tst", "-k", "10"};
cmdLineOptions.add_options()
("key,k", po::value<Unsigned>(&v)->required())
;
po::store(po::command_line_parser(sizeof(args) / sizeof(args[0]), args).options(cmdLineOptions).run(), vm);
po::notify(vm);
std::cout << v << '\n';
return 0;
}
原因可能是MSVC的著名破2阶段查找
The cause is likely MSVC's famously broken 2-phase lookup
- Two phase lookup - explanation needed
这篇关于升压program_options自定义验证程序不与海湾合作委员会的工作,作品有MSVC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!