Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Automatic template deduction C++20 with aggregate type

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

It compiles with GCC 10 in C++20 standard (but not in C++17 standard)
and latest MSVC.

This is because GCC 10 and the latest MSVC implement allow initializing aggregates from a parenthesized list of values, which allows us to use parentheses to initialize aggregates.

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

This is because GCC 10 implements class template argument deduction for aggregates, which makes T automatically deduced to int.


Please note that clang does not currently implement these two C++20 features, so your code cannot be accepted by clang.

You can refer to cppreference to get the current compiler’s support for C++20 features.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading