4

According to P1814R0, the template deduction should work for alias with default value. With GCC 12.2(-std=c++20), the following code built successfully. However, in MSVC v19.33(/std:c++20) (which supports P1814R0), I got an error

<source>(10): error C2641: cannot deduce template arguments for 'Matrix3'

Is this a MSVC bug or I missed some configurations in MSVC?

Test codes:

template <typename Type, int Row, int Col, int Options = 0>
class Matrix {
    Type storage[Row * Col];
};

template <typename Type = double>
using Matrix3 = Matrix<Type, 3, 3>;

int main() {
    Matrix3 a;
    return 0;
}

https://godbolt.org/z/nbfaxY7vs

Yingnan Wu
  • 43
  • 4

1 Answers1

1

The syntax for saying: I don't want to provide template arguments, just use the defaults, should be:

Matrix3<> a;

Indeed C++20 adopted P1814 into section over.match.class.deduct, so it seems that the following should be valid since C++20:

Matrix3 a;

GCC

As the OP mentions in a comment GCC rejected the above in C++17 and accepts it in C++20.


MSVC

As mention by @康桓瑋 MSVC accepts since C++20 only the form:

Matrix3 a{};

but still rejects:

Matrix3 a;

Clang

Clang still rejects both.


To Summarize

It seems that GCC is updated for C++20 on that respect, MSVC did part of the way and Clang is lagging behind.

Amir Kirsh
  • 12,564
  • 41
  • 74
  • 3
    By adding <> the compiler is forced to use default template argument. I think there is no CTAD here anymore. I changed gcc standard to -std=c++17, and actually gcc gave me an error ```error: alias template deduction only available with '-std=c++20' or '-std=gnu++20'```. Since clang does not implement P1814R0, the clang output seems reasonable to me. However, MSVC claims to implement P1814R0, but I got some unexpected error. – Yingnan Wu Feb 13 '23 at 05:42