【发布时间】:2016-12-02 10:42:53
【问题描述】:
以下代码编译运行正常。
void foo() {
}
template <typename T, typename... Args>
void foo(T x, Args... args) {
cout << x << endl;
foo(args...);
}
// inside main()
foo(1,1,1);
其他代码无法编译:
void foo() {
}
template <typename... Args, typename T>
void foo(Args... args, T x) {
foo(args...);
cout << x << endl;
}
// inside main()
foo(1,1,1);
编译器说没有匹配的函数来调用foo(1,1,1) 并说foo(Args... args, T x) 是候选,但模板参数推导/替换失败,因为候选需要1 个参数,但提供了3 个。
这种情况是否存在任何编译器无法处理的歧义?这个编译错误对我来说似乎不合逻辑。也许这不符合 C++ 标准?
【问题讨论】:
标签: c++ templates c++11 variadic-templates variadic-functions