3

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.

  • clang 14.x rejects it with the following error:

<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


Which compiler is behaving correctly here?

cigien
  • 57,834
  • 11
  • 73
  • 112
Vittorio Romeo
  • 90,666
  • 33
  • 258
  • 416
  • Related/near-dupe: [Template argument deduction for parenthesized initialization of aggregates in C++](https://stackoverflow.com/questions/69961473/template-argument-deduction-for-parenthesized-initialization-of-aggregates-in-c). – dfrib Jan 11 '22 at 10:30

1 Answers1

8

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.

The code is valid, but Clang just doesn't support P1816 yet.

Adding a deduction guide allows this to compile in Clang as well.

template <typename B> D(B) -> D<B>;
cigien
  • 57,834
  • 11
  • 73
  • 112