我不熟悉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;
};我能理解SFINAE是如何工作的以及特征是如何工作的。在引用优先选择之后,找到了“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> {};“is_integral”有类似的实现吗?多么?
发布于 2017-04-23 13:49:17
我们从这里获得了这样的信息:
检查T是否为整型。提供成员常量值,如果T是
bool、char、char16_t、char32_t、wchar_t、short、int、long、long long或任何实现定义的扩展整数类型,包括任何有符号、无符号和cv限定的变量,则该值等于true。否则,值等于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>> {};
// ...请注意,std::false_type和std::true_type是std::integral_constant的专门化。有关更多细节,请参见这里。
https://stackoverflow.com/questions/43571962
复制相似问题