【发布时间】:2011-11-15 16:38:54
【问题描述】:
将函数模板特化的地址传递给常规模板函数没有问题:
template <typename T>
void f(T) {}
template <typename A, typename B>
void foo(A, B) {}
int main()
{
foo(&f<int>, &f<float>);
}
但是,当我尝试将相同的特化传递给可变参数模板时:
template <typename T>
void f(T) {}
template <typename... A>
void bar(A...) {}
int main()
{
bar(&f<int>, &f<float>);
}
使用 GCC 时出现以下编译器错误(我尝试了 4.6.1 和 4.7.0):
test.cpp: In function 'int main()':
test.cpp:9:27: error: no matching function for call to 'bar(<unresolved overloaded function type>, <unresolved overloaded function type>)'
test.cpp:9:27: note: candidate is:
test.cpp:5:6: note: template<class ... A> void bar(A ...)
test.cpp:5:6: note: template argument deduction/substitution failed:
为什么会出现这些错误?
【问题讨论】:
-
auto a = &f<int>也不起作用:error: 'a' has incomplete type -
这可行:
bar((void(*)(int))f<int>, (void(*)(double))f<double>);但显然这不是解决方案。它只是意味着(就像错误所说的那样)由于某种原因它无法分辨&f<int>是什么类型。
标签: c++ templates c++11 variadic-templates template-function