Reputation: 5472
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
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