我目前正在使用 Boost.ProgramOptions 进行项目,我必须创建以下结构来为选项添加一些约束:
template <const char *str1, const char*... str2>
struct restrictedValues
{
...
};
为了验证该新选项,您必须重载该boost::program_options::validate
函数:
template<class T, class charT>
void validate(boost::any& v, const std::vector< std::basic_string<charT> >& xs, T*, long);
对此验证函数的调用如下:
validate(value_store, new_tokens, (T*)0, 0);
正如 boost 所指出的那样:“目标类型是通过一个参数指定的,该参数具有指向所需类型的指针类型。对于没有部分模板排序的编译器,这是一种解决方法,就像最后一个 'long/int' 参数一样。”
因此,我以以下方式编写了我的验证版本:
template<class charT, const char *... str>
void validate(boost::any &v,
const std::vector<std::basic_string<charT> >& values,
restrictedValues<str...>* /*target_type*/,
int /*unused*/) { ... }
看起来我的 clang 版本(Ubuntu clang 版本 3.5.0-4ubuntu2~trusty2 (tags/RELEASE_350/final)(基于 LLVM 3.5.0))只是跳过我的版本并在默认版本中优雅地失败。虽然我的 gcc ((Ubuntu 4.8.2-19ubuntu1) 4.8.2) 愉快地编译。
编辑查看显示不同行为的实时示例,感谢@dyp:
#include <boost/any.hpp>
#include <vector>
#include <string>
#include <iostream>
template <const char *str1, const char*... str2> struct restrictedValues
{
/*...*/
};
template<class T, class charT>
void validate(boost::any&, const std::vector< std::basic_string<charT> >&, T*, long)
{
std::cout << "default version\n";
}
extern char const client[] = "hello";
extern char const server[] = "world";
template<class charT, const char *... str>
void validate(boost::any &,
const std::vector<std::basic_string<charT> >&,
restrictedValues<str...>* /*target_type*/,
int /*unused*/) {
std::cout << "custom version\n";
}
int main()
{
boost::any a;
std::vector<std::string> xs;
restrictedValues<client, server>* p = 0;
validate(a, xs, p, 0);
}
此外,对结构/函数使用非可变模板(固定数量的 const char*)的相同过程确实像一个魅力。
我不太确定哪个查找过程会导致这种模棱两可的错误。如果我的函数没有使用模板,它会根据重载规则进行选择,但事实并非如此。通过阅读模板函数的部分排序规则,两个函数对模板参数具有相同的特化,但我的期望是 int/long 技巧应该起作用。关于如何解决这个模板之谜的任何想法?