Search code examples
c++templatesvisual-c++c++20ctad

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


Solution

  • 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.