Reputation: 257
I have a struct
template <typename A, typename B>
struct foo {static const int type = 0;};
I want the implementation to be different when both A
and B
are arithmetic. However this did not work.
template <typename A, typename B, typename std::enable_if<
std::is_arithmetic<A>::value &&
std::is_arithmetic<B>::value, bool>::type = 0>
struct foo<A, B> {static const int type = 1;}
I get the compiler error default template arguments may not be used in partial specializations
. So how can I make this work?
One thing to keep in mind is that I also want to have other simpler template specializations that might look like this.
template <>
struct foo<string, vector<int>> {static const int type = 2;}
In summary, I want the first definition to be like a default, and then define a bunch of specialized implementations, one of which is kind of general.
Upvotes: 2
Views: 486
Reputation: 172924
You could define the primary template, partial specialization and full specialization separately like
// the primary template
template <typename A, typename B, typename = void>
struct foo {static const int type = 0;};
// the partial specialization
template <typename A, typename B>
struct foo<A, B, typename std::enable_if<
std::is_arithmetic<A>::value &&
std::is_arithmetic<B>::value>::type> {static const int type = 1;};
// the full specialization
template <>
struct foo<string, vector<int>, void> {static const int type = 2;};
Upvotes: 4