Skip to content

Latest commit

 

History

History
93 lines (66 loc) · 3.3 KB

type traits Part.2.md

File metadata and controls

93 lines (66 loc) · 3.3 KB

Primary type categories

image.png

std::is_integral<> 为例:

  /// is_integral
  template<typename _Tp>
    struct is_integral
    : public __is_integral_helper<__remove_cv_t<_Tp>>::type
    { };

is_integral 继承自 __is_integral_helper<> ,首先,移除掉 _Tpconst/volatile/const volatile 属性,然后使用 __is_integral_helper<>::type 判断 _Tp 是否是 integer 类型。

__is_integral_helper<> 是一个类模板,默认情况下 typefalse_type,然后针对不同类型进行模板特化bool/char/int/long 等特化类型都是返回 true_type 。因此在模板实例化时,这些类型都会匹配到相应的特化模板,得到的 type 就是 true_type

  template<typename>
    struct __is_integral_helper
    : public false_type { };

  template<>
    struct __is_integral_helper<bool>
    : public true_type { };

  template<>
    struct __is_integral_helper<char>
    : public true_type { };

  template<>
    struct __is_integral_helper<signed char>
    : public true_type { };

  template<>
    struct __is_integral_helper<unsigned char>
    : public true_type { };

// 此处省略一万行

  template<>
    struct __is_integral_helper<short>
    : public true_type { };

  template<>
    struct __is_integral_helper<unsigned short>
    : public true_type { };

  template<>
    struct __is_integral_helper<int>
    : public true_type { };

  template<>
    struct __is_integral_helper<unsigned int>
    : public true_type { };

  template<>
    struct __is_integral_helper<long>
    : public true_type { };

  template<>
    struct __is_integral_helper<unsigned long>
    : public true_type { };

// 此处省略一万行

Composite type categories

image.png

Type properties

image.png

Type relationships

image.png

Const-volatility specifiers

image.png

References

image.png

Miscellaneous transformations

image.png

Links

  1. https://en.cppreference.com/w/cpp/header/type_traits
  2. https://en.cppreference.com/w/cpp/types/is_integral
  3. https://en.cppreference.com/w/cpp/types/decay
  4. https://zhuanlan.zhihu.com/p/98106799