Template argument deduction in case of designated initializers in C++

  aggregate-initialization, c++, c++20, language-lawyer

In the following code there is an initialization of A<T> objects with template argument deduction using designated initializers in two slightly distinct forms:

template<typename T>
struct A { T t; };

int main() {
   A a{.t=1};   //#1: ok in GCC and MSVC
   A b{.t={1}}; //#2: ok in MSVC only
}

The first way is accepted by both GCC and MSVC, while the second one is ok for MSVC only while GCC prints errors:

error: class template argument deduction failed:
error: no matching function for call to 'A(<brace-enclosed initializer list>)'

Demo: https://gcc.godbolt.org/z/PaEaMjM7q

Which compiler is right there?

Source: Windows Questions C++

LEAVE A COMMENT