C++11:type_traits (2) Composite

2020-02-06  本文已影响0人  fck_13
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);
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);
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);
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);
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);
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);
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);

(未完待续)

上一篇下一篇

猜你喜欢

热点阅读