2

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?

Silicomancer
  • 8,604
  • 10
  • 63
  • 130

1 Answers1

5

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; };
bolov
  • 72,283
  • 15
  • 145
  • 224