C++11 std::是如何实现的?

C++11 std::是如何实现的?,c++11,templates,typetraits,C++11,Templates,Typetraits,我不熟悉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> ty

我不熟悉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;
};
模板
结构是不可积的
{
静态常量布尔值/*=如果T是整数,则为真,否则为假*/;
typedef std::整型_常量类型;
};
我能理解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> {};
templatestruct是\u指针\u助手:std::false\u type{};
模板struct是\u指针\u助手:std::true\u类型{};
模板struct是指针:是指针\u助手{};
“is_integral”是否有类似的实现?如何实现?

从中我们可以看出:

检查T是否为整数类型。如果T是类型
bool
char
char16\u T
char32\u T
wchar\u T
short
int
long
long
,或任何实现定义的扩展整数,则提供等于true的成员常量值r类型,包括任何有符号、无符号和cv限定变量。否则,值等于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>> {};

// ...
模板结构是_integral_base:std::false_type{};
模板结构是_integral_base:std::true_type{};
模板结构是_integral_base:std::true_type{};
模板结构是_integral_base:std::true_type{};
模板结构是_积分:是_积分_基{};
// ...

请注意,
std::false_type
std::true_type
std::integral_常量的特化。有关详细信息,请参阅。

您还需要删除cv限定符,因为
is_integral
需要为它们返回true。以及处理有符号/无符号。@Nicolas Right,让我更新答案。Thank you.std::integral_constant的专门化
@skypjack关于实现定义的扩展整数类型呢?@L.F.我很冷静,但我看不出这种改进有什么帮助。问题是std::is_integral是如何实现的?这里的答案考虑了几种类型来演示它。你可以说我没有考虑<代码> W查尔特特< /代码>,但是添加它不会给答案本身添加任何东西。我的两分钱。