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
checks if a type is void
(class template)
checks if a type is integral type
(class template)
checks if a type is floating-point type
(class template)
checks if a type is an array type
(class template)
checks if a type is an enumeration type
(class template)
checks if a type is an union type
(class template)
checks if a type is a class type (but not union type)
(class template)
checks if a type is a function type
(class template)
checks if a type is a pointer type
(class template)
checks if a type is lvalue reference
(class template)
checks if a type is rvalue reference
(class template)
checks if a type is a pointer to a non-static member object
(class template)
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}
/// 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就是通过元编程的手段,以及编译器的辅助来实现的。
- C++11标准库之Type Traits简介
- STL之四 type traits
- type traits 之"本质论"
- Type Traits
- type traits
- 模版元编程之——Type Traits
- Type Traits 实例,提高效率
- C++ Type traits
- C++ Type traits
- C++ Type traits
- 【转】C++ type traits
- c++ type traits
- c++ type traits 2
- [c++]traits
- 【c++11】static_assert: 静态断言 type traits:类型特征
- C++ 11标准STL中Traits的is_pointer的实现
- PHP SPL标准库之SplFixedArray简介
- PHP SPL标准库之SplStack简介
- 软件测试入门
- YII 框架 第十天(1) urlManager 地址美化
- 2013.9月,百度电话面试经验
- layout_weight体验(实现按比例显示)
- C++ 11右值引用
- C++11标准库之Type Traits简介
- ContentProvider/ContentResolver和Uri详解
- 二叉树相关问题
- OpenCV:概述、结构和内容
- HDOJ 1540 && POJ 2892 —— 线段树
- hdu2873 Bomb Game
- Ubuntu下如何将普通用户提升到root权限
- 深信服笔试题
- 应用程序正常初始化(0xc0000135)失败