【发布时间】:2018-07-02 11:52:46
【问题描述】:
我正在尝试使用可变参数模板来重构我的一些代码,但编译器出现“没有匹配的调用函数”错误。下面是一个简化的版本(它可能对功能没有意义,但是一个重现错误的例子):
// base case
void testFunc(int i) { std::cout << i << std::endl; }
template <class T, class... Args> void testFunc(int i) {
T t = 0;
std::cout << t << std::endl;
testFunc<Args...>(i);
}
int main() {
testFunc<int, long, float>(1);
return 0;
}
错误信息:
main.cpp:9:3: error: no matching function for call to 'testFunc'
testFunc<Args...>(i);
^~~~~~~~~~~~~~~~~
main.cpp:9:3: note: in instantiation of function template specialization 'testFunc<float>' requested here
main.cpp:9:3: note: in instantiation of function template specialization 'testFunc<long, float>' requested here
main.cpp:13:3: note: in instantiation of function template specialization 'testFunc<int, long, float>' requested here
testFunc<int, long, float>(1);
^
main.cpp:6:40: note: candidate template ignored: couldn't infer template argument 'T'
template <class T, class... Args> void testFunc(int i) {
^
1 error generated.
看起来模板参数的展开是有效的,并在基本情况下停止。但我已经定义了基本情况。为什么没有匹配功能?
【问题讨论】:
标签: c++ c++11 templates variadic-templates template-argument-deduction