以下代码编译并运行正常.
void foo() {
}
template <typename T, typename... Args>
void foo(T x, Args... args) {
cout << x << endl;
foo(args...);
}
// inside main()
foo(1,1,1);
Run Code Online (Sandbox Code Playgroud)
这个其他代码不编译:
void foo() {
}
template <typename... Args, typename T>
void foo(Args... args, T x) {
foo(args...);
cout << x << endl;
}
// inside main()
foo(1,1,1);
Run Code Online (Sandbox Code Playgroud)
编译器说调用没有匹配函数,foo(1,1,1)并且说它foo(Args... args, T x)是候选函数,但模板参数推导/替换失败,因为候选者需要1个参数,但提供了3个参数.
这种情况是否存在任何编译器无法处理的歧义?这个编译错误对我来说似乎不合逻辑.也许这与C++标准有意无关?