Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Partial specialization of non-type parameter

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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?

>Solution :

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading