gpt4 book ai didi

c++ - "template argument deduction for class templates"是否应该为可变类模板推导出空参数包?

转载 作者:可可西里 更新时间:2023-11-01 17:40:02 25 4
gpt4 key购买 nike

“类模板的模板参数推导” 提案 ( P0091R2 ) 包含以下示例:

template<class ... Ts> struct X { X(Ts...) };
X x1{1}; // OK X<int>
X x11; // OK X<>

(除了构造函数定义缺少主体这一事实之外),该示例似乎表明用零参数构造的可变参数类模板将被推导为一个空的参数包。

很遗憾,最新版本的g++不同意:

int main()
{
X x1{1};
X x11;
}

 In function 'int main()':
error: invalid use of template-name 'X' without an argument list
X x11;
^
note: class template argument deduction requires an initializer

example on wandbox


我在提案中找不到阐明这种互动的明确措辞。 g++ 这里错了吗?

最佳答案

这在 P0620R0 之后现在是良构的在 C++17 发布之前删除了引用的限制。

保留以前的答案以供引用:


N4618 [dcl.type.class.deduct]/1 :

If a placeholder for a deduced class type appears as a decl-specifier in the decl-specifier-seq of a simple-declaration, the init-declarator of that declaration shall be of the form

declarator-id attribute-specifier-seqopt initializer

初始化器不是可选的。

关于c++ - "template argument deduction for class templates"是否应该为可变类模板推导出空参数包?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40556822/

25 4 0