wimalopaan
wimalopaan

Reputation: 5472

class template specialization with constraint / concept

I try to use a constraint for a class specialization:

struct A {};
struct B {};
struct C {};

template<typename T>
concept bool AorB() {
    return std::is_same<T, A>::value || std::is_same<T, B>::value;
}

template<typename T>
class X {};

template<AorB T>
class X {};

int main() {
    X<A> x1; // error: redeclaration 'template<class T> class X' with different constraints class X {};
    X<B> x2;
    X<C> x3;
}

I don't know if I am making a mistake here or if this generally won't be possible?

What could be alternatives to this approach? I could make to specializations using CRTP to a common base template, but thats looks ugly to me.

Upvotes: 12

Views: 2604

Answers (1)

Quentin
Quentin

Reputation: 63154

This isn't a specialization, you actually redeclared the primary template, which is indeed an error.
A specialization would look like this:

template<typename T>
class X { };

template<AorB T>
class X<T> { };
//     ^^^

Upvotes: 21

Related Questions