yui*_*ank 8 templates type-traits c++11
我不熟悉cpp中的模板魔法.在阅读了这个链接中的 "TemplateRex"之后,我对std :: is_intergral的工作原理感到困惑.
template< class T >
struct is_integral
{
static const bool value /* = true if T is integral, false otherwise */;
typedef std::integral_constant<bool, value> type;
};
Run Code Online (Sandbox Code Playgroud)
我可以理解SFINAE的工作原理以及特征的工作原理.在引用cppreference之后,找到了'is_pointer'的实现,而不是'is_integral',它看起来像这样:
template< class T > struct is_pointer_helper : std::false_type {};
template< class T > struct is_pointer_helper<T*> : std::true_type {};
template< class T > struct is_pointer : is_pointer_helper<typename std::remove_cv<T>::type> {};
Run Code Online (Sandbox Code Playgroud)
'is_integral'有类似的实现吗?怎么样?
从这里我们得到:
检查T是否为整数类型.提供构件的恒定值,它等于真,如果T是类型
bool,char,char16_t,char32_t,wchar_t,short,int,long,long long,或任何实现所定义的扩展整型,包括任何符号,无符号,和CV-合格的变体.否则,value等于false.
这样的事情可能就是你可以实现它的方式:
template<typename> struct is_integral_base: std::false_type {};
template<> struct is_integral_base<bool>: std::true_type {};
template<> struct is_integral_base<int>: std::true_type {};
template<> struct is_integral_base<short>: std::true_type {};
template<typename T> struct is_integral: is_integral_base<std::remove_cv_t<T>> {};
// ...
Run Code Online (Sandbox Code Playgroud)
请注意,std::false_type并且std::true_type是std::integral_constant.的特化.有关详细信息,请参见此处