Specialization of class template with auto parameter

HD86 26 Reputation points
2020-10-18T16:10:39.153+00:00

The following does not compile with the latest MSVC.

template <auto>
struct S {};

template <int *V>
struct S<V> {};

int main()
{
    S<1>{}; //error C2440: 'specialization': cannot convert from 'int' to 'int *'
}

I thought perhaps an auto parameter is ignored in overload resolution, so I tried the following, which still did not compile to my surprise.

template <auto>
struct S {};

template <int V>
struct S<V> {};

template <int *V>
struct S<V> {};

int main()
{
    S<1>{}; //error C2752: 'S<1>': more than one partial specialization matches the template argument list
}

I think that the second case must be a compiler error. Is this correct?

C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,690 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Jeanine Zhang-MSFT 9,661 Reputation points Microsoft Vendor
    2020-10-19T02:12:55.697+00:00

    Hi,

    This syntax is valid in the C++ Concepts Technical Specification, but not in C++20. In C++20 concepts, auto is only permitted at the top level in a function parameter type.

    For more details I suggest you could refer to the links:
    https://stackoverflow.com/questions/60358154/auto-as-a-template-argument-placeholder-for-a-function-parameter
    https://stackoverflow.com/questions/38026884/advantages-of-auto-in-template-parameters-in-c17

    As far as I'm concerned you could try to use c++17 in the the latest MSVC. (Priperties -> General -> C++ language Standard -> ISO C++17 Standard(std:c++17)).

    Best Regards,

    Jeanine


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.