C++11标准库之Type Traits简介

来源:互联网 发布:英语软件哪个比较好 编辑:程序博客网 时间:2024/05/20 07:31

虽然可以追溯到TR1及boost库中,Type Traits在C++11的标准库中才被C++语言正式引入的。直接地翻译,Type Traits就是“类型的特征”的意思。在C++元编程中,程序员不少时候都需要了解一些类型的特征信息,并根据这些类型信息选择应有的操作。

在《深入理解C++11》一书中,我们就常使用Type Traits来判断类型的特性。比如:

#include <type_traits>template<typename T>constexpr bool is_pod(T) {    return std::is_pod<T>::value;}

这里就定义了一个名为is_pod的函数模板。该函数模板只是type_traits中模板类is_pod的简单包装。通过该函数,我们可以判断一个类型的数据是否为POD类型的:

int main(){    int a;    std::cout << is_pod(a) << std::endl;}

值得注意的是,Type Traits是用于元编程中的元素,而且我们的函数表达式使用了constexpr进行修饰,那么这就意味着程序员可以在编译时期就获得is_pod返回的值。在本例中,我们获得的值为true(1),那么上面的main函数在运行时的代码将与:

int main(){    std::cout << 1 << std::endl;}

等价。

在C++11标准(也是TR1)的提案中(N1424),语言设计者在<type_traits>头文件中定义了大量如上的Type Traits。下表摘自http://en.cppreference.com/w/cpp/types:


Primary type categories


is_void
(C++11)

checks if a type is void 
(class template)


is_integral
(C++11)

checks if a type is integral type 
(class template)


is_floating_point
(C++11)

checks if a type is floating-point type 
(class template)


is_array
(C++11)

checks if a type is an array type 
(class template)


is_enum
(C++11)

checks if a type is an enumeration type 
(class template)


is_union
(C++11)

checks if a type is an union type 
(class template)


is_class
(C++11)

checks if a type is a class type (but not union type) 
(class template)


is_function
(C++11)

checks if a type is a function type 
(class template)


is_pointer
(C++11)

checks if a type is a pointer type 
(class template)


is_lvalue_reference
(C++11)

checks if a type is lvalue reference 
(class template)


is_rvalue_reference
(C++11)

checks if a type is rvalue reference 
(class template)


is_member_object_pointer
(C++11)

checks if a type is a pointer to a non-static member object 
(class template)


is_member_function_pointer
(C++11)

checks if a type is a pointer to a non-static member function 
(class template)

 
这里我们只摘取了一部分。在最初的设计中,C++语言设计者为的Type Traits进行了简单的分类。不过在后来的语言标准中,Type Traits也几经修正演化,也不是由一个文档能够观察全貌的。所幸的是上面的链接应该提供了最新的Type Traits的内容。

除去判断类型的特性,<type_traits>中我们也可以找到is_same这样的比较两个类型是否相等的类模板,以及enable_if这样的根据bool值选择类型的类模板,读者可以从上面链接中寻找其使用方式。


而从实现上讲,这些Type Traits通常是通过模板特化的元编程手段来完成的,比如在g++ 4.8.1的<type_traits>头文件中我们可以找到以下代码:

  /// is_const  template<typename>    struct is_const    : public false_type { };    // 版本 1  template<typename _Tp>    struct is_const<_Tp const>    : public true_type { };    // 版本 2

这里的false_type和true_type则是两个helper class,其定义如下:

  /// integral_constant  template<typename _Tp, _Tp __v>    struct integral_constant    {      static constexpr _Tp                  value = __v;      typedef _Tp                           value_type;      typedef integral_constant<_Tp, __v>   type;      constexpr operator value_type() { return value; }    };  template<typename _Tp, _Tp __v>    constexpr _Tp integral_constant<_Tp, __v>::value;  /// The type used as a compile-time boolean with true value.  typedef integral_constant<bool, true>     true_type;  /// The type used as a compile-time boolean with false value.  typedef integral_constant<bool, false>    false_type;


如果不想细看代码,也可以简单地说,true_type和false_type就是包含一个静态类成员value的类模板,其静态成员一个为true,一个为false,仅此而已。这样一来,通过特化,如果我们使用const类型作为模板is_const类型参数,则可以获得其常量静态成员value的值为true(1)。这是因为模板在实例化的时候选择了“版本2”。反过来,如果模板实例化到“版本1”,则value常量静态成员为false(0)。如下例所示:

#include <iostream>#include <type_traits>int main(){    int a;    const int b = 3;    std::cout << std::is_const<decltype(a)>::value << std::endl;    // 1    std::cout << std::is_const<decltype(b)>::value << std::endl;    // 0}


此外,还有一点值得指出,并非所有的Type Traits都能够使用上面的元编程的手段来实现。C++语言设计者在实践中进行了一些考量,让部分的Type Traits实现为了intrinsic,简单地说,就是要编译器辅助来计算出其值。我们可以看看g++4.8.1中POD的定义:

  /// is_pod  // Could use is_standard_layout && is_trivial instead of the builtin.  template<typename _Tp>    struct is_pod    : public integral_constant<bool, __is_pod(_Tp)>    { };
这里的__is_pod就是编译器内部的intrinsic。事实上,在C++11中,编译器必须辅助实现很多Type Traits的模板类,C++11标准中这些Type Traits模板类如下所示:

template <class T> struct is_class;template <class T> struct is_union;template <class T> struct is_enum;template <class T> struct is_polymorphic;template <class T> struct is_empty;template <class T> struct has_trivial_constructor;template <class T> struct has_trivial_copy;template <class T> struct has_trivial_assign;template <class T> struct has_trivial_destructor;template <class T> struct has_nothrow_constructor;template <class T> struct has_nothrow_copy;template <class T> struct has_nothrow_assign;template <class T> struct is_pod;template <class T> struct is_abstract;
总的来说,Type Traits就是通过元编程的手段,以及编译器的辅助来实现的。

原创粉丝点击