C++11:type_traits (1) primary ty

2020-02-05  本文已影响0人  fck_13

当我第一次看《STL源码剖析》的时候,我就觉得type traits是stl的基础,是一个很有趣,很值得学习的东西。为了使C++成为modern C++,标准委员会在C++ 11 中添加了很多新的内容,type traits是其中非常重要的一部分。
note: 为了方便,代码都写在google test的测试函数中

template< class T >
struct is_void;

Tvoidconst voidvolatile void或者 const volatile void的时候,其静态成员变量value的值为true,否则为false
代码示例:

EXPECT_TRUE(std::is_void<void>::value);
EXPECT_FALSE(std::is_void<int>::value);

大家看到这个例子可能会有疑问,int类型是非void类型是显而易见的,为什么还需要std::is_void。例子中只是最简单的一种形式,在实际的开发中,我们为了能够让变量或者类型的名字更加准确,会使用typedef 或者 using 来给他们重命名,这样就很难知道具体类型是什么了。例如

typede void not-direct-void-name;
...
...
//需要知道not-direct-void-name是否是void
if(std::is_void<not-direct-void-name>::value)
{
  ...
}
else
{
  ...
}
template< class T >
struct is_integral;

T的类型为为 boolcharchar8_tchar16_tchar32_twchar_tshortintlong,或者long long类型时,value的值为true,其余为false。在类型前边添加修饰前缀signed或者unsigned不影响value的值。

EXPECT_TRUE((std::is_integral<int>::value));
EXPECT_TRUE((std::is_integral<char>::value));
EXPECT_FALSE((std::is_integral<float>::value));
EXPECT_FALSE((std::is_integral<double>::value));
EXPECT_FALSE((std::is_integral<std::string>::value));
EXPECT_TRUE((std::is_integral<bool>::value));
template< class T >
struct is_floating_point;

Tfloatdouble或者long double的时候,value的值为true,否则为false
代码示例:

EXPECT_FALSE((std::is_floating_point<int>::value));
EXPECT_TRUE((std::is_floating_point<double>::value));
template< class T >
struct is_array;

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

class A {};
EXPECT_FALSE(std::is_array<A>::value);
EXPECT_TRUE(std::is_array<A[]>::value);
EXPECT_TRUE(std::is_array<A[3]>::value);
EXPECT_FALSE(std::is_array<float>::value);
EXPECT_FALSE(std::is_array<int>::value);
EXPECT_TRUE(std::is_array<int[]>::value);
EXPECT_TRUE(std::is_array<int[3]>::value);
template< class T >
struct is_enum;

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

class A {};
enum E {};
enum class Ec : int {};

EXPECT_FALSE(std::is_enum<A>::value);
EXPECT_TRUE(std::is_enum<E>::value);
EXPECT_TRUE(std::is_enum<Ec>::value);
EXPECT_FALSE(std::is_enum<int>::value);
template< class T >
struct is_union;

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

struct A {};

typedef union
{
    int a;
    float b;
} B;

struct C
{
    B d;
};

EXPECT_FALSE(std::is_union<A>::value);
EXPECT_TRUE(std::is_union<B>::value);
EXPECT_FALSE(std::is_union<C>::value);
EXPECT_FALSE(std::is_union<int>::value);
template< class T >
struct is_class;

T为类类型(非联合)时,value的值为true,否则为false
代码示例如下:

struct A {};
class B {};
enum class C {}
EXPECT_TRUE(std::is_class<A>::value);
EXPECT_TRUE(std::is_class<B>::value);
EXPECT_FALSE(std::is_class<C>::value);
EXPECT_FALSE(std::is_class<int>::value);
struct A
{
    int fun() const&;
};

int f();

EXPECT_FALSE(std::is_function<A>::value);
EXPECT_TRUE(std::is_function<int(int)>::value);
EXPECT_TRUE(std::is_function<decltype(f)>::value);
EXPECT_FALSE(std::is_function<int>::value);

using T = func::PM_traits<decltype(&A::fun)>::member_type; // T is int() const&
EXPECT_TRUE(std::is_function<T>::value);
template< class T >
struct is_pointer;

T为指向对象或者指向函数的指针类型(但不是指向成员和成员函数)时,value的值为true,否则为false
代码示例如下:

class A {};

EXPECT_FALSE(std::is_pointer<A>::value);
EXPECT_TRUE(std::is_pointer<A*>::value);
EXPECT_FALSE(std::is_pointer<A&>::value);
EXPECT_FALSE(std::is_pointer<int>::value);
EXPECT_TRUE(std::is_pointer<int*>::value);
EXPECT_TRUE(std::is_pointer<int**>::value);
EXPECT_FALSE(std::is_pointer<int[10]>::value);
EXPECT_FALSE(std::is_pointer<std::nullptr_t>::value);
template< class T >
struct is_lvalue_reference;

T为左值引用类型时,value的值为true,否则为false
代码示例为:

class A {};

EXPECT_FALSE(std::is_lvalue_reference<A>::value);
EXPECT_TRUE(std::is_lvalue_reference<A&>::value);
EXPECT_FALSE(std::is_lvalue_reference<A&&>::value);
EXPECT_FALSE(std::is_lvalue_reference<int>::value);
EXPECT_TRUE(std::is_lvalue_reference<int&>::value);
EXPECT_FALSE(std::is_lvalue_reference<int&&>::value);
template< class T >
struct is_rvalue_reference;

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

class A {};

EXPECT_FALSE(std::is_rvalue_reference<A>::value);
EXPECT_FALSE(std::is_rvalue_reference<A&>::value);
EXPECT_TRUE(std::is_rvalue_reference<A&&>::value);
EXPECT_FALSE(std::is_rvalue_reference<int>::value);
EXPECT_FALSE(std::is_rvalue_reference<int&>::value);
EXPECT_TRUE(std::is_rvalue_reference<int&&>::value);
template< class T >
struct is_member_object_pointer;

T为指向非静态数据成员的指针类型时,value的值为true,否则为false
代码示例如下:

class cls {};
EXPECT_TRUE(std::is_member_object_pointer<int(cls::*)>::value);
EXPECT_FALSE(std::is_member_object_pointer<int(cls::*)()>::value);
template< class T >
struct is_member_function_pointer;

当为指向非静态成员函数的指针类型时,value的值为true,否则为false
代码示例如下:

class A
{
public:
    void member() { }
};

EXPECT_TRUE(std::is_member_function_pointer<decltype(&A::member)>::value);

(未完待续)

上一篇 下一篇

猜你喜欢

热点阅读