我需要你的帮助来找出为什么以下代码无法编译。
#include <iostream>
template <int I>
void foo(){
std::cout << I << std::endl;
std::cout << "end of list" << std::endl;
}
template <int I, int ... Ints>
void foo(){
std::cout << I << std::endl;
foo<Ints...>();
}
int main(){
foo<1, 2>();
return 0;
}
我遇到了这个错误。
function_parameter_pack.cpp: In instantiation of ‘void foo() [with int I = 1; int ...Ints = {2}]’:
function_parameter_pack.cpp:17:13: required from here
function_parameter_pack.cpp:12:15: error: call of overloaded ‘foo<2>()’ is ambiguous
foo<Ints...>();
~~~~~~~~~~~~^~
function_parameter_pack.cpp:4:6: note: candidate: void foo() [with int I = 2]
void foo(){
^~~
function_parameter_pack.cpp:10:6: note: candidate: void foo() [with int I = 2; int ...Ints = {}]
void foo(){
不是应该选择更专业的功能吗?即只有一个模板(第一个)。
最佳答案
foo<2>
同样匹配两个模板(因为 int...
也匹配零个 int
s)这就是编译器提示歧义的原因。
Isn't a more specialized function is supposed to be chosen?
是的,但这些都同样特别。通过查看函数的参数而不是模板参数来推断哪个是更专业的匹配函数。
解决这个问题的一种方法是使第二个函数模板采用 2 个或更多模板参数:
#include <iostream>
template<int I>
void foo() {
std::cout << I << std::endl;
std::cout << "end of list" << std::endl;
}
template<int I, int J, int... Ints>
void foo() {
std::cout << I << std::endl;
foo<J, Ints...>();
}
int main() {
foo<1>();
foo<1, 2>();
foo<1, 2, 3>();
}
关于c++ - 使用可变参数模板打印列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66222696/