为什么我的gcc-5.4.0
如果它们没有出现在函数的参数列表的末尾,则不能推导出参数包?虽然以works
的正确方式推断了对works<int,int,int>
的调用,但并未推断出对fails
的调用,而是假设只有空参数包。导致关于函数提供的参数太多的错误消息。
#include <iostream>
template <typename...args_t>
void works (int first, args_t...args) {
std::cout << __PRETTY_FUNCTION__ << std::endl;
}
template <typename...args_t, typename last_t, typename=void>
void fails (args_t...args, last_t last) {
std::cout << __PRETTY_FUNCTION__ << std::endl;
}
int main () {
works (0, 1, 2, 3);
fails (0, 1, 2, 3);
return 0;
}
编辑:作为答案解释,不允许在参数包之后有类型名称。但根据cppreference.com,如果可以推导出其他模板参数,那么它应该是有效的。显然,给定的示例不能使用我的gcc
进行编译。相反,对于太多给定的参数,它仍然存在相同的错误。
#include <iostream>
template <typename...args_t, typename U, typename=void>
static int valid (args_t...args, U u) {
std::cout << __PRETTY_FUNCTION__ << std::endl;
return u;
}
int main () {
return valid(0, 0.0, -1, 3u);
}
答案 0 :(得分:1)
为什么我的
gcc-5.4.0
如果它们没有出现在函数的参数列表的末尾,则不能推导出参数包?
因为你不能指定超出variadic参数包的任何类型
void fails (args_...args, int last)
// ^^^^^^^^^^
这只是与默认参数值或普通省略号(...
)相同的问题,那些需要在参数列表的末尾打开(或者说它们必须是最后一个元素) :
void fails(int x = 0, int last);
void fails(int x, ..., int last);