美文网首页
C++11:type_traits (2) Composite

C++11:type_traits (2) Composite

作者: fck_13 | 来源:发表于2020-02-06 01:16 被阅读0次
    • std::is_fundamental : 判断一个类型是否是一个基础类型,基础类型为算术类型, void, 或者nullptr_t
      可能的实现为:
    template< class T >
    struct is_fundamental
      : std::integral_constant<
            bool,
            std::is_arithmetic<T>::value ||
            std::is_void<T>::value  ||
            std::is_same<std::nullptr_t, typename std::remove_cv<T>::type>::value
    > {};
    

    T为算数类型,void或者nullptr_t时,且这些类型可以带有const和volatile的修饰符,value的值为true,否则为false
    代码示例如下:

    class A {};
    
    EXPECT_FALSE(std::is_fundamental<A>::value);
    EXPECT_TRUE(std::is_fundamental<int>::value);
    EXPECT_FALSE(std::is_fundamental<int&>::value);
    EXPECT_FALSE(std::is_fundamental<int*>::value);
    EXPECT_TRUE(std::is_fundamental<float>::value);
    EXPECT_FALSE(std::is_fundamental<float&>::value);
    EXPECT_FALSE(std::is_fundamental<float*>::value);
    
    • std::is_arithmetic : 判断一个类型是否为算数类型。算数类型包括整型和浮点型(可加constvolatile修饰)。
      可能的实现为:
    template< class T >
    struct is_arithmetic : std::integral_constant<bool,
                                                  std::is_integral<T>::value ||
                                                  std::is_floating_point<T>::value> {};
    

    T为算数类型时,value的值为true,否则为false
    代码示例如下:

    class A {};
    
    EXPECT_FALSE(std::is_arithmetic<A>::value);
    EXPECT_TRUE(std::is_arithmetic<bool>::value);
    EXPECT_TRUE(std::is_arithmetic<int>::value);
    EXPECT_TRUE(std::is_arithmetic<int const>::value);
    EXPECT_FALSE(std::is_arithmetic<int&>::value);
    EXPECT_FALSE(std::is_arithmetic<int*>::value);
    EXPECT_TRUE(std::is_arithmetic<float>::value);
    EXPECT_TRUE(std::is_arithmetic<float const>::value);
    EXPECT_FALSE(std::is_arithmetic<float&>::value);
    EXPECT_FALSE(std::is_arithmetic<float*>::value);
    EXPECT_TRUE(std::is_arithmetic<char>::value);
    EXPECT_TRUE(std::is_arithmetic<char const>::value);
    EXPECT_FALSE(std::is_arithmetic<char&>::value);
    EXPECT_FALSE(std::is_arithmetic<char*>::value);
    
    • std::is_scalar : 判断一个类型是否为scaler 类型(标量类型)。scalar type : 可能时带有constvolatile修饰的算数类型,指针,指向数据成员的指针,枚举,或者std::nullptr_t
      可能的实现为:
    template< class T >
    struct is_scalar : std::integral_constant<bool,
                         std::is_arithmetic<T>::value     ||
                         std::is_enum<T>::value           ||
                         std::is_pointer<T>::value        ||
                         std::is_member_pointer<T>::value ||
                         std::is_null_pointer<T>::value> {};
    

    T为标量类型时,value的值为true,否则为false

    class cls {};
    EXPECT_TRUE(std::is_scalar<int>::value);
    EXPECT_FALSE(std::is_scalar<cls>::value);
    
    • std::is_object : 判断一个类型是否为对象类型。对象类型是几个类型的统称,即标量类型,数组,联合或者类类型。
      可能的实现为:
    template< class T>
    struct is_object : std::integral_constant<bool,
                         std::is_scalar<T>::value ||
                         std::is_array<T>::value  ||
                         std::is_union<T>::value  ||
                         std::is_class<T>::value> {};
    

    代码示例如下:

    class cls {};
    
    EXPECT_TRUE(std::is_object<int>::value);
    EXPECT_FALSE(std::is_object<int&>::value);
    EXPECT_TRUE(std::is_object<cls>::value);
    EXPECT_FALSE(std::is_object<cls&>::value);
    
    • std::is_compound:判断一个类型是否为组合类型。组合类型为可能被constvolatile修饰的数组,函数,对象指针,函数指针,成员对象指针,成员函数指针,引用,类,联合或者枚举。
      可能的实现:
    template< class T >
    struct is_compound : std::integral_constant<bool, !std::is_fundamental<T>::value> {};
    

    代码示例如下:

    class cls {};
    EXPECT_TRUE(std::is_compound<cls>::value);
    EXPECT_FALSE(std::is_compound<int>::value);
    
    • std::is_reference :判断一个类型是否为左值引用或者右值引用。
      可能的实现:
    template <class T> struct is_reference : std::false_type {};
    template <class T> struct is_reference<T&>: std::true_type{};
    template <class T> struct is_reference<T&&> : std::true_type {};
    

    代码示例如下:

    class A {};
    
    EXPECT_FALSE(std::is_reference<A>::value);
    EXPECT_TRUE(std::is_reference<A&>::value);
    EXPECT_TRUE(std::is_reference<A&&>::value);
    EXPECT_FALSE(std::is_reference<int>::value);
    EXPECT_TRUE(std::is_reference<int&>::value);
    EXPECT_TRUE(std::is_reference<int&&>::value);
    
    • std::is_member_pointer : 判断一个类型是否为非静态成员函数或者对象
      可能的实现:
    template< class T >
    struct is_member_pointer_helper : std::false_type{};
    
    
    template< class T, class U >
    struct is_member_pointer_helper<T U::*> : std::true_type{};
    
    template< class T >
    struct is_member_pointer : 
        is_member_pointer_helper<typename std::remove_cv<T>::type> {};
    

    代码示例如下:

    class cls {};
    EXPECT_TRUE(std::is_member_pointer<int(cls::*)>::value);
    EXPECT_FALSE(std::is_member_pointer<int>::value);
    

    (未完待续)

    相关文章

      网友评论

          本文标题:C++11:type_traits (2) Composite

          本文链接:https://www.haomeiwen.com/subject/ebtyxhtx.html