Home > database >  Partial specialization of non-type parameter
Partial specialization of non-type parameter

Time:10-19

I understand how specialization works for type/pointer to type:

template <typename T> class TestClass
{
public:
    TestClass() { std::cout << "TestClass: general" << std::endl; }
};

template <typename T> class TestClass<T*>
{
public:
    TestClass() { std::cout << "TestClass: pointer" << std::endl; }
};

TestClass<int> tc1; //TestClass: general
TestClass<int*> tc2; // TestClass: pointer

but if we have

template<auto N> class S
{
public:
    S() { std::cout << "auto template" << std::endl; }
};

template<char N> class S<N>
{
public:
    S() { std::cout << "char template" << std::endl; }
};

If I use it like:

S<20U> s1; //char template
S<'c'> s2; //char template

in both case I get char template;

What is the meaning of writing

template<char N> class S<N>

and when the 'auto' template will be called then?

CodePudding user response:

when the 'auto' template will be called then?

The primary template template<auto N> class S will be used whenever the non-type parameter N is of type other than char. This means that it will be used for

S<20U> s1; //auto template

GCC Demo

Output:

auto template
char template

Here is the clang bug:

Clang chooses specialization over primary template for non-type template parameter

Here is the msvc bug:

MSVC chooses specialization over primary template for non-type template parameter

  •  Tags:  
  • c
  • Related