Template argument deduction for parenthesized initialization of aggregates 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 two forms distinct by the type of braces:

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

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

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

error C2641: cannot deduce template arguments for 'A'
error C2780: 'A<T> A(void)': expects 0 arguments - 1 provided
error C2784: 'A<T> A(A<T>)': could not deduce template argument for 'A<T>' from 'int'

Demo: https://gcc.godbolt.org/z/97G1acqPr

Is it a bug in MSVC?

Source: Windows Questions C++

LEAVE A COMMENT