【发布时间】:2013-02-03 21:35:22
【问题描述】:
我有这段代码(从更复杂的版本简化而来):
template <class... A1> class Test {
public:
template <class... A2> void print (void(*function)(A2...,A1...)) {
}
};
void test_print (int a, float b, double c) {
}
int main () {
Test<float,double> test;
test.print<int> (&test_print);
}
如果我在 GCC 4.6.3 上使用 g++ -std=c++0x filename.cpp 编译它可以正常编译,但是在 clang 3.0 上使用 clang++ -std=c++0x filename.cpp 它会引发以下错误:
filename.cpp:14:10: error: no matching member function for call to 'print'
test.print<int> (&test_print);
~~~~~^~~~~~~~~~
filename.cpp:3:33: note: candidate template ignored: failed template argument deduction
template <class... A2> void print (void(*function)(A2...,A1...)) {
^
1 error generated.
在 GCC 4.7.2 上也有错误:
filename.cpp: In function 'int main()':
filename.cpp:14:33: error: no matching function for call to 'Test<float, double>::print(void (*)(int, float, double))'
filename.cpp:14:33: note: candidate is:
filename.cpp:3:33: note: template<class ... A2> void Test::print(void (*)(A2 ..., A1 ...)) [with A2 = {A2 ...}; A1 = {float, double}]
filename.cpp:3:33: note: template argument deduction/substitution failed:
filename.cpp:14:33: note: mismatched types 'float' and 'int'
现在的问题是:为什么会失败或者我做错了什么?
【问题讨论】:
-
您是否尝试删除
<int>? C++ 尽可能使用模板类型推导。 -
g++ 4.7 错误信息:ideone.com/qwSKJl 参数排序似乎有问题。
-
@leemes:我试过了,没用
-
有趣的是,这有效:ideone.com/iVVRKK。闻起来像虫子。
-
我也认为这是 g++ 中的一个错误。 @eyelash 我感谢你提供了一个最小的例子(这在堆栈溢出时非常罕见);)
标签: c++ c++11 variadic-templates