MSVC 2012 通过 SFINAE 检测模板函数的模板参数数量
MSVC 2012 detecting number of template arguments of a template function via SFINAE
我正在尝试做的事情:我有一个模板对象传入,作为接口的一部分,应该有一个 "process" 定义了一些参数的函数(我不知道多少)其中一些是模板参数。
即
struct A { static void process(int a); };
struct B { template <typename B0> static void process(int a, B0 b0); };
都是要接收的有效处理程序。所以现在我需要检测处理程序的签名:静态类型参数和一些模板参数。
为此,我使用了一些模板魔术技巧,这些技巧可能会缩小到有问题的部分 - 检测多个模板参数(或只是检索模板签名)。
我试图找出所需信息的方法是使用 Is it possible to write a template to check for a function's existence?
中描述的方法检查明确专用的签名
struct _D;
template <typename T>
struct get_template_args_count
{
private:
template <int count> struct R { enum { value = count }; };
template <typename C>
static R<0> retrieve(decltype(&C::process));
template <typename C>
static R<1> retrieve(decltype(&C::template process<_D>));
template <typename C>
static R<-1> retrieve(...);
public:
typedef decltype(retrieve<T>(nullptr)) Result;
enum { value = Result::value };
};
int main(int argc, char* argv[])
{
std::cout
<< "A::process " << get_template_args_count<A>::value << "\n"
<< "B::process " << get_template_args_count<B>::value << "\n";
std::cin.get();
return 0;
}
使用 clang(使用 msvc2013 或 linux 版本构建,使用 gcc-4.9.2 构建)编译并输出:
A::process 0
B::process 1
使用 msvc2012 也可以编译,但输出:
A::process 0
B::process -1
当通过注释后备案例(带有 (...) 的案例)走投无路时,msvc2012 吓坏了:
main.cpp(28): error C2893: Failed to specialize function template 'get_template_args_count<T>::R<count> get_template_args_count<T>::retrieve(unknown)'
with [ T=B, count=1 ]
With the following template arguments: 'B'
v:\test\test\test\main.cpp(63) : see reference to class template instantiation 'get_template_args_count<T>' being compiled
with [ T=B ]
main.cpp(28): error C2893: Failed to specialize function template 'get_template_args_count<T>::R<count> get_template_args_count<T>::retrieve(unknown)'
with [ T=B, count=0 ]
With the following template arguments: 'B'
main.cpp(29): error C2825: 'get_template_args_count<T>::Result': must be a class or namespace when followed by '::'
with [ T=B ]
main.cpp(29): error C2039: 'value' : is not a member of '`global namespace''
main.cpp(29): error C2275: 'get_template_args_count<T>::Result' : illegal use of this type as an expression
with [ T=B ]
main.cpp(29): error C2146: syntax error : missing '}' before identifier 'value'
main.cpp(29): error C2143: syntax error : missing ';' before '}'
main.cpp(29): error C2365: 'value' : redefinition; previous definition was 'enumerator'
main.cpp(29) : see declaration of 'value'
(日志稍微重新格式化以减少行数)
我也尝试过使用上述问题的评论中描述的不同技术(使用 char[sizeof],使用 typeof 并将检查移动到 return 类型),但无济于事 - 它要么产生相同的结果或因更奇怪的错误而分崩离析(包括 "unexpected end-of-file",没有明显的原因)。
我也用另一种技术(通过 SFINAE 比较原型)检查了一个类似的问题 Deduce variadic args and return type from functor template parameter (MSVC-specific),但是当我不知道确切的签名(即我不知道静态参数的数量和类型)。当然,我可能会针对手头的特定任务强行使用它们,但是...
所以我有两个问题:
- 为什么 MSVC 总是这么难?.. 好吧,这是一个修辞,不需要回答。
- 我是否滥用了 clang/gcc 的善意,而实际上 MSVC 通过向我抛出无意义的错误来做正确的事情?除了强制所有可能的 static/template 参数组合并使用完整签名原型比较它们之外,是否有任何解决方法或正确的方法来做到这一点?
这里的问题是 MSVC 拒绝提取 &C::template process<_D>
的类型以用于重载解析(注意没有任何有意义的错误消息)。它似乎将实例化函数视为仅一个函数的重载集;可能是一个实现错误。
您可以通过将重载集输入函数参数来强制它将重载集转换为函数指针类型:
template<typename T>
T* fn_id(T* func) {
return nullptr;
}
一旦 T
被扁平化为函数类型,您就可以将其用于 decltype
。
template <typename C>
static R<1> retrieve(decltype(fn_id(&C::template process<_D>)));
通过这个 hack,我得到了与使用 clang 时相同的输出。
我正在尝试做的事情:我有一个模板对象传入,作为接口的一部分,应该有一个 "process" 定义了一些参数的函数(我不知道多少)其中一些是模板参数。
即
struct A { static void process(int a); };
struct B { template <typename B0> static void process(int a, B0 b0); };
都是要接收的有效处理程序。所以现在我需要检测处理程序的签名:静态类型参数和一些模板参数。
为此,我使用了一些模板魔术技巧,这些技巧可能会缩小到有问题的部分 - 检测多个模板参数(或只是检索模板签名)。
我试图找出所需信息的方法是使用 Is it possible to write a template to check for a function's existence?
中描述的方法检查明确专用的签名struct _D;
template <typename T>
struct get_template_args_count
{
private:
template <int count> struct R { enum { value = count }; };
template <typename C>
static R<0> retrieve(decltype(&C::process));
template <typename C>
static R<1> retrieve(decltype(&C::template process<_D>));
template <typename C>
static R<-1> retrieve(...);
public:
typedef decltype(retrieve<T>(nullptr)) Result;
enum { value = Result::value };
};
int main(int argc, char* argv[])
{
std::cout
<< "A::process " << get_template_args_count<A>::value << "\n"
<< "B::process " << get_template_args_count<B>::value << "\n";
std::cin.get();
return 0;
}
使用 clang(使用 msvc2013 或 linux 版本构建,使用 gcc-4.9.2 构建)编译并输出:
A::process 0
B::process 1
使用 msvc2012 也可以编译,但输出:
A::process 0
B::process -1
当通过注释后备案例(带有 (...) 的案例)走投无路时,msvc2012 吓坏了:
main.cpp(28): error C2893: Failed to specialize function template 'get_template_args_count<T>::R<count> get_template_args_count<T>::retrieve(unknown)'
with [ T=B, count=1 ]
With the following template arguments: 'B'
v:\test\test\test\main.cpp(63) : see reference to class template instantiation 'get_template_args_count<T>' being compiled
with [ T=B ]
main.cpp(28): error C2893: Failed to specialize function template 'get_template_args_count<T>::R<count> get_template_args_count<T>::retrieve(unknown)'
with [ T=B, count=0 ]
With the following template arguments: 'B'
main.cpp(29): error C2825: 'get_template_args_count<T>::Result': must be a class or namespace when followed by '::'
with [ T=B ]
main.cpp(29): error C2039: 'value' : is not a member of '`global namespace''
main.cpp(29): error C2275: 'get_template_args_count<T>::Result' : illegal use of this type as an expression
with [ T=B ]
main.cpp(29): error C2146: syntax error : missing '}' before identifier 'value'
main.cpp(29): error C2143: syntax error : missing ';' before '}'
main.cpp(29): error C2365: 'value' : redefinition; previous definition was 'enumerator'
main.cpp(29) : see declaration of 'value'
(日志稍微重新格式化以减少行数)
我也尝试过使用上述问题的评论中描述的不同技术(使用 char[sizeof],使用 typeof 并将检查移动到 return 类型),但无济于事 - 它要么产生相同的结果或因更奇怪的错误而分崩离析(包括 "unexpected end-of-file",没有明显的原因)。
我也用另一种技术(通过 SFINAE 比较原型)检查了一个类似的问题 Deduce variadic args and return type from functor template parameter (MSVC-specific),但是当我不知道确切的签名(即我不知道静态参数的数量和类型)。当然,我可能会针对手头的特定任务强行使用它们,但是...
所以我有两个问题:
- 为什么 MSVC 总是这么难?.. 好吧,这是一个修辞,不需要回答。
- 我是否滥用了 clang/gcc 的善意,而实际上 MSVC 通过向我抛出无意义的错误来做正确的事情?除了强制所有可能的 static/template 参数组合并使用完整签名原型比较它们之外,是否有任何解决方法或正确的方法来做到这一点?
这里的问题是 MSVC 拒绝提取 &C::template process<_D>
的类型以用于重载解析(注意没有任何有意义的错误消息)。它似乎将实例化函数视为仅一个函数的重载集;可能是一个实现错误。
您可以通过将重载集输入函数参数来强制它将重载集转换为函数指针类型:
template<typename T>
T* fn_id(T* func) {
return nullptr;
}
一旦 T
被扁平化为函数类型,您就可以将其用于 decltype
。
template <typename C>
static R<1> retrieve(decltype(fn_id(&C::template process<_D>)));
通过这个 hack,我得到了与使用 clang 时相同的输出。