template-specializationc++20c++-conceptsclass-templategcc10

Why does conceptual class template specialization cause an error


I tried to build the following with gcc 10 -std=gnu++20 -fconcepts:

template <std::signed_integral T>
class MyClass{ T a; };

template <std::unsigned_integral T>
class MyClass{ T a; };

Why does this code cause the following error?

> declaration of template parameter ‘class T’ with different constraints
> 55 | template <std::unsigned_integral T>
>       |           ^~~

Shouldn't it be fine?


Solution

  • Shouldn't it be fine?

    No, constraints don't make classes "overloadable". You still need a primary template and then you need to specialize that template:

    template <std::integral T>
    class MyClass;
    
    template <std::signed_integral T>
    class MyClass<T>{ T a; };
    
    template <std::unsigned_integral T>
    class MyClass<T>{ T a; };