简体   繁体   English

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

[英]clang vs gcc - CTAD of struct deriving from template parameter

Consider the following code:考虑以下代码:

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

D d{[]{ }};
  • gcc 12.x accepts it and deduces d to be D</* type of lambda */> as expected. gcc 12.x 接受它并按预期推断dD</* type of lambda */>

  • clang 14.x rejects it with the following error: 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 godbolt.org 上的实时示例


Which compiler is behaving correctly here?哪个编译器在这里表现正确?

In the code snippet, no deduction guide has been provided.在代码片段中,没有提供任何扣除指南。 P1816 added deduction guides for aggregate class templates in C++20, by requiring that an aggregate deduction candidate is generated. P1816为 C++20 中的聚合 class 模板添加了推导指南,要求生成聚合推导候选

The code is valid, but Clang just doesn't support P1816 yet.代码有效,但 Clang还不支持 P1816

Adding a deduction guide allows this to compile in Clang as well.添加推导指南也可以在 Clang 中编译

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Clang vs GCC - Variadic模板参数包后跟带默认值的参数适用于GCC 4.8但不适用于Clang 3.5 - Clang vs GCC - Variadic template parameter pack followed by parameter with default value works in GCC 4.8 but not Clang 3.5 Clang 或 GCC 在拒绝/接受此 CTAD 代码时是否正确? - Is Clang or GCC correct in rejecting/accepting this CTAD code? 使用 constexpr 函数的结果作为模板参数(clang vs gcc) - using result of constexpr function as a template parameter (clang vs gcc) Consexpr function 对模板参数 object 的评估(MSVC 与 clang/gcc) - Constexpr function evaluation on a template parameter object (MSVC vs clang/gcc) clang vs gcc中的空结构的默认初始化器 - Default Initializer of empty struct in clang vs gcc GCC &amp; Clang vs MSVC Bug,同时在函数模板的相同参数子句中扩展模板参数包 - GCC & Clang vs MSVC Bug while expanding template parameter pack in the same parameter clause for function templates 使用多个模板参数包应用 CTAD - Applying CTAD with multiple template parameter packs 在clang vs gcc和msvc中按方法指针的模板 - Template by method pointer in clang vs gcc and msvc 函数模板作为成员 - GCC 与 CLANG - function template as member - GCC vs. CLANG GCC vs Clang & MSVC 使用非类型模板参数时出现错误 - GCC vs Clang & MSVC Bug while using non type template parameter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM