gpt4 book ai didi

c++ - 具有固定数量的模板参数的类内部的多个包扩展

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:33:55 25 4
gpt4 key购买 nike

这段代码格式是否正确?即使 Ts 很可能为空,函数模板本身的声明也会在 clang 和 gcc 中给出错误。

// 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'

最佳答案

[温度.res]/8:

If every valid specialization of a variadic template requires an empty template parameter pack, the template is ill-formed, no diagnostic required.

f 的每个有效特化都要求 Ts 是一个空包。因此该程序是格式错误的 NDR。两个编译器都是正确的。

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

关于c++ - 具有固定数量的模板参数的类内部的多个包扩展,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32918009/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com