c++ - clang vs gcc - 从模板参数派生的结构的 CTAD

标签 c++ language-lawyer c++20 ctad deduction-guide

考虑以下代码:

template <typename B>
struct D : B { };

D d{[]{ }};
  • gcc 12.x 接受它并推导出 d成为 D</* type of lambda */>正如预期的那样。

  • clang 14.x 拒绝它并出现以下错误:

<source>:4:3: error: no viable constructor 
              or deduction guide for deduction of template arguments of 'D'
D d{[]{ }};
  ^

<source>:2:8: note: candidate template ignored: 
              could not match 'D<B>' against '(lambda at <source>:4:5)'
struct D : B { };
       ^

<source>:2:8: note: candidate function template not viable: 
              requires 0 arguments, but 1 was provided

live example on godbolt.org


哪个编译器在这里表现正确?

最佳答案

在代码片段中,没有提供任何扣除指南。 P1816为 C++20 中的聚合类模板添加了推导指南,要求生成一个聚合推导候选

代码有效,但 Clang 只是 doesn't support P1816还没有。

添加扣除指南允许 compile in Clang也是。

template <typename B> D(B) -> D<B>;

关于c++ - clang vs gcc - 从模板参数派生的结构的 CTAD,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70660641/

相关文章:

c++ - 为虚拟机 C++ 实现堆栈

c++ - 什么是执行宽字符集及其编码?

c++ - 将概念传递给函数

c++ - 概念-如何限制积分模板值

c++ - 如何让 CLion 在保存时自动构建

c++ - 为什么这里不调用拷贝构造函数?

c - C 中的初始化与赋值

c++ - 变量的 cstyle 转换返回的值是纯右值还是左值?

c++ - 正确使用 std::map 作为类成员

c++ - 为什么在 c++17 中不推荐使用 std::allocator 的构造和销毁函数?