Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Template deduction for template aliasing with default value

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

like image 804
Yingnan Wu Avatar asked Oct 17 '25 00:10

Yingnan Wu


1 Answers

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.

like image 84
Amir Kirsh Avatar answered Oct 19 '25 13:10

Amir Kirsh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!