简体   繁体   English

满足条件的多种类型的模板类专门化

[英]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? 如果不以任何方式修改Type ,是否有一种简单的方法可以为匹配编译时条件的所有类型进行专门化? 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): 例如,如果我想为所有整数类型专门化Type ,我想做类似这样的事情(只有一些有效的东西):

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

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> { /* ... */ };

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM