asc
asc

Reputation: 45

Template class specialization for multiple types satisfying a condition

If I have a template class, like so:

template<typename T>
class Type { /* ... */ };

Without modifying Type in any way, is there a simple way to specialize it for all such types that match a compile-time condition? For example, if I wanted to specialize Type for all integral types, I'd like to do something like this (only something that works, that is):

template<typename T>
class Type<std::enable_if<std::is_integral<T>, T>::type> { /* ... */ };

Upvotes: 4

Views: 536

Answers (1)

Pubby
Pubby

Reputation: 53047

This should work:

template<typename T, bool B = std::is_integral<T>::value>
class Type;

// doesn't have to be a specialization, although I think it's more clear this way
template<typename T>
class Type<T, false> { /* ... */ };

template<typename T>
class Type<T, true> { /* ... */ };

Upvotes: 6

Related Questions