【问题标题】:Is "template argument deduction for class templates" supposed to deduce empty parameter packs for variadic class templates?“类模板的模板参数推导”是否应该为可变参数类模板推导空参数包?
【发布时间】:2016-11-11 21:55:03
【问题描述】:

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

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

(除了构造函数定义缺少一个主体),这个例子似乎暗示了一个用零参数构造的variadic class template将被推导出来一个空的参数包。

很遗憾,最新版本的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++错了吗?

【问题讨论】:

  • X x11{}; 工作。
  • 我想说 gcc 的实现现在正处于实验阶段。即vector v{2,3} 有效,但vector v{2,3,4} 产生'int' is not a class, struct, or union type 的错误。
  • @krzaq:它似乎试图为这种特殊情况调用constructor (2)。我想知道这是否真的是有意的(即在这些情况下消歧的规则是什么)。顺便说一句,我知道 gcc 处于实验阶段 - 我的兴趣是在适当的情况下提交错误报告。
  • @VittorioRomeo 向量填充了值 2 和 3,而不是 3 和 3。如果你用 vector v(2,3) 初始化它,它会调用你提到的构造函数
  • 你不喜欢“通用构造”语法吗?比上一个更令人困惑...

标签: c++ templates language-lawyer c++17 template-argument-deduction


【解决方案1】:

P0620R0 在 C++17 发布之前删除引用的限制之后,现在格式正确。

上一个答案留作参考:


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

如果推导的类类型的占位符显示为 decl-specifiersimple-declarationdecl-specifier-seq 中,它的 init-declarator声明的格式为

declarator-id attribute-specifier-seqoptinitializer

初始化器不是可选的。

【讨论】:

  • 不错。这是一个非常方便的网站!
  • @Yakk 是的,虽然它看起来像 they are tweaking the thing 所以也许我得尽快编辑它......
猜你喜欢
  • 2013-09-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多