std::is_integral是如何实现的?

How is std::is_integral implemented?

我不熟悉 cpp 中的模板魔法。在阅读了'TemplateRex'在这个link中所说的内容后,我对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 的工作原理以及特征的工作原理。在引用 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> {};

'is_integral'有类似的实现吗?怎么样?

here 我们得到:

Checks whether T is an integral type. Provides the member constant value which is equal to true, if T is the type bool, char, char16_t, char32_t, wchar_t, short, int, long, long long, or any implementation-defined extended integer types, including any signed, unsigned, and cv-qualified variants. Otherwise, value is equal to 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_typestd::true_typestd::integral_constant 的特化。有关详细信息,请参阅 here