Automatic template deduction C++20 with aggregate type

  aggregate-type, c++, c++20, templates

I am puzzled about this C++ code:

template <class T>
struct Foo {
  T value;
};
 
int main() {
    return Foo<int>(0).value;
    // Below code works as well in gcc
    // return Foo(0).value;
}

It compiles with GCC 10 in C++20 standard (but not in C++17 standard) and latest MSVC, but not with clang 13 or 14, even in C++20.

According to the standard (from cppreference) it should be possible to instantiate Foo at least when specifying the templated type.

Why is this related to C++20 ? I see nothing that change in the template deduction specification (I maybe missed something).

Also (this is strange), GCC in C++20 mode even compiles when we call Foo without specifying templated type (Foo(0)).

godbolt link here

Source: Windows Questions C++

LEAVE A COMMENT