类中的多个包展开具有固定数量的模板参数

Multiple pack expansions inside class with fixed number of template arguments

本文关键字:参数 包展开      更新时间:2023-10-16

代码格式正确吗?函数模板本身的声明在clang和gcc中都给出了错误,即使Ts很可能是空的。

// error: too many template arguments for class template 'pair'
template<class I, class U, class... Ts>
void f(std::pair<I,U,Ts...>);
int main()
{
    f(std::pair<int,int>());
}

函数调用在gcc中给出了这个没有意义的错误。没有转换到int:

note: cannot convert 'std::pair<int, int>()' (type 'std::pair<int, int>') to type 'int'

[temp.res]/8:

如果可变模板的每个有效特化都需要一个空的模板参数包,模板格式错误,无诊断必需的。

f的每个有效专门化都要求Ts是一个空包。因此,该程序是病态的NDR。两个编译器都是正确的

对于GCC的诊断,这似乎是由于它习惯使用int作为占位符,用于"看起来像类型但没有意义的东西",以用于错误恢复。