__type_traits详解
来源:互联网 发布:设计相关软件 编辑:程序博客网 时间:2024/06/07 22:15
在STL中为了提供通用的操作而又不损失效率,我们用到了一种特殊的技巧,叫traits编程技巧。具体的来说,traits就是 通过定义一些结构体或类,并利用模板类特化和偏特化的能力,给类型赋予一些特性,这些特性根据类型的不同而异。在程序设计中可以使用这些traits来判 断一个类型的一些特性,引发C++的函数重载机制,实现同一种操作因类型不同而异的效果。traits的编程技巧极度弥补了C++语言的不足 。
举例:
现在定义一个__type_traits可以获得类型的如下属性:
1. 是否存在non-trivial default constructor
2. 是否存在non-trivial copy constructor
3. 是否存在non-trivial assignment operator
4. 是否存在non-trivial destructor
struct __true_type {
};
struct __false_type {
};
template <class _Tp>
struct __type_traits {
typedef __false_type has_trivial_default_constructor;
typedef __false_type has_trivial_copy_constructor;
typedef __false_type has_trivial_assignment_operator;
typedef __false_type has_trivial_destructor;
};
问题:为什么把对象的所有的属性都定义为__false_type?
这样是采用最保守的做法,先把所有的对象属性都设置为__false_type,然后在针对每个基本数据类型设计特化的__type_traits,就可以达到预期的目的,如可以定义__type_traits<int>如下:
template <>
struct __type_traits<int> {
typedef __true_type has_trivial_default_constructor;
typedef __true_type has_trivial_copy_constructor;
typedef __true_type has_trivial_assignment_operator;
typedef __true_type has_trivial_destructor;
};
template <>
struct __type_traits<char> {
typedef __true_type has_trivial_default_constructor;
typedef __true_type has_trivial_copy_constructor;
typedef __true_type has_trivial_assignment_operator;
typedef __true_type has_trivial_destructor;
};
......
......
其他基本类型的traits也可以有相应的定义
__type_traits的偏特化版本
template <class _Tp>
struct __type_traits<_Tp*> {
typedef __true_type has_trivial_default_constructor;
typedef __true_type has_trivial_copy_constructor;
typedef __true_type has_trivial_assignment_operator;
typedef __true_type has_trivial_destructor;
typedef __true_type is_POD_type;
};
我们可以自定义__type_traits的特化版本
比如对与自定义的Shape类型,我们可以这样定义__type_traits<Shape>
struct __type_traits<Shape> {
typedef __false_type has_trivial_default_constructor;
typedef __true_type has_trivial_copy_constructor;
typedef __true_type has_trivial_assignment_operator;
typedef __true_type has_trivial_destructor;
typedef __true_type is_POD_type;
};
如果编译器够厉害,我们甚至可以不用自己去定义特化的__type_traits,编译器就能够帮我们搞定:)
如何使用呢?
假设现在用个模板函数fun需要根据类型T是否有non-trivial constructor来进行不同的操作,可以这样来实现:
template<class T>
void fun()
{
typedef typename __type_traits<T>::has_trivial_constructor _Trivial_constructor;
__fun(_Trivial_constructor()); // 根据得到的_Trivial_constructor来调用相应的函数
}
// 两个重载的函数
void _fun(_true_type)
{
cout<<"fun(_true_type)called"<<endl;
}
void _fun(_false_type)
{
cout<<"fun(_false_type) called"<<endl;
}
//测试代码
int main()
{
fun<char>();
fun<int>();
fun<char *>();
fun<double>();
}
- __type_traits详解
- __type_traits
- __type_traits
- __type_traits 编程纪要
- 类型萃取器__type_traits
- iterator_traits和__type_traits
- 神奇的__type_traits
- “类型萃取器”__type_traits
- STL 神奇的__type_traits (转
- 关于__type_traits的设计思想
- STL之__normal_iterator和__type_traits
- STL源码分析之__type_traits
- 【STL源码剖析】__type_traits技法
- STL学习_萃取技术__type_traits
- C++ traits编程技法之__type_traits
- 从__type_traits知道的c++的一种用法
- STL — SGT STL的私房菜: __type_traits
- 《STL源码剖析》学习笔记2——神奇的__type_traits
- JQuery入门—编写一个简单的JQuery应用案例
- HDOJ2045 不容易系列之(3)—— LELE的RPG难题
- PHP-redis中文文档
- 楼市“王炸”!住建部:租购同权将立法!
- retrofit下上传文件+获取和发送cookie
- __type_traits详解
- 【2017多校】HDU6047 Maximum Sequence 【贪心】
- java.net.MalformedURLException 异常
- OC温故:面向对象的三大特性(封装,继承,多态)
- ScrollView 源码解析
- JavaScript的基本概念----语句
- MySQL必知必会笔记(五)事务处理 全球化和本地化(字符集和校对)
- 2017 Multi-University Training Contest
- 在centos下搭建hustoj教程