【STL源码学习】std::list类的类型别名分析
有了点模板元编程的traits基础,看STL源码清晰多了,以前看源码的时候总被各种各样的typedef给折腾得看不下去,
将<list>头文件的类继承结构简化如下
#include <xmemory>
#include <stdexcept> #define _STD_BEGIN namespace std {
#define _STD_END } _STD_BEGIN // 第一个模板参数
template <class _Val_types>
class _List_val : public _Container_base { }; // 第二个模板参数
template <class _Ty, class _Alloc0>
struct _List_base_types { }; template <bool _Al_has_storage, class _Alloc_types>
class _List_alloc
: public _List_val<typename _Alloc_types::_Val_types> { }; template <class _Ty, class _Alloc> // 这里默认_Alloc不为空类型
class _List_buy // !is_empty<_Alloc>::value暂不讨论
: public _List_alloc<true, _List_base_types<_Ty, _Alloc>> { }; template <class _Ty, class _Alloc = allocator<_Ty> >
class list : public _List_buy<_Ty, _Alloc> { }; _STD_END
举个例子,看看list<int>这个实例化会产生什么效果,从下往上看。
_Ty被替换成int,_Alloc默认被替换成allocator<int>,上一层基类_List_buy的两个模板参数也是_Ty和_Alloc
再上一层基类_List_alloc有2个模板参数,第一个是bool值,编译期判断是否为空类型(empty class),第二个则是由_Ty和_Alloc两个模板参数实例化的_List_base_types类,该类没有基类型,这个类如同名字所说,list的基本类型。
_List_alloc的基类_List_val也是由_List_base_types作为模板参数实例化,_List_val的基类Container_base0仅仅包含两个函数体内为空的函数。
结论:list<>进行实例化后,模板参数_Ty是通过_List_base_types来定义list各类中的类型别名。
现在来看看_List_base_types内部的typedef
template<class _Ty,
class _Alloc0>
struct _List_base_types
{ // types needed for a container base
typedef _Alloc0 _Alloc;
typedef _List_base_types<_Ty, _Alloc> _Myt; typedef _Wrap_alloc<_Alloc> _Alty0;
typedef typename _Alty0::template rebind<_Ty>::other _Alty; typedef typename _Get_voidptr<_Alty, typename _Alty::pointer>::type
_Voidptr;
typedef _List_node<typename _Alty::value_type,
_Voidptr> _Node; typedef typename _Alty::template rebind<_Node>::other _Alnod_type;
typedef typename _Alnod_type::pointer _Nodeptr;
typedef _Nodeptr& _Nodepref; typedef typename _If<_Is_simple_alloc<_Alty>::value,
_List_simple_types<typename _Alty::value_type>,
_List_iter_types<typename _Alty::value_type,
typename _Alty::size_type,
typename _Alty::difference_type,
typename _Alty::pointer,
typename _Alty::const_pointer,
typename _Alty::reference,
typename _Alty::const_reference,
_Nodeptr> >::type
_Val_types;
};
_Myt为实例化后的该类模板的别名。
先看看最后的关键的_Val_types,变量类型,通过元函数(Meta Function)_If来完成类型计算。
template<bool,
class _Ty1,
class _Ty2>
struct _If
{ // type is _Ty2 for assumed false
typedef _Ty2 type;
}; template<class _Ty1,
class _Ty2>
struct _If<true, _Ty1, _Ty2>
{ // type is _Ty1 for assumed true
typedef _Ty1 type;
};
通过模板特化来完成编译期的判断,三个模板参数,第一个为bool,如果第一个参数为true,那么类型type是_Ty1,否则类型type是_Ty2。
_Val_types第一个参数<_Is_simple_alloc<_Alty>::value,判断_Alty是否为“简单的内存分配器”,如果不是,则意味着你专门定制了特殊的内存分配器(类),并将这个类的类型别名(size_type、pointer等等)传递给_Val_types。
如果是,就直接用_List_simple_types,也就是list的简单类型。
template<class _Ty>
struct _List_simple_types
: public _Simple_types<_Ty>
{ // wraps types needed by iterators
typedef _List_node<_Ty, void *> _Node;
typedef _Node *_Nodeptr;
};
到这里就可以发现,链表类必须用到的结点类就在这里:_List_node<_Ty, void *>
template<class _Value_type,
class _Voidptr>
struct _List_node
{ // list node
_Voidptr _Next; // successor node, or first element if head
_Voidptr _Prev; // predecessor node, or last element if head
_Value_type _Myval; // the stored value, unused if head private:
_List_node& operator=(const _List_node&);
}; template<class _Value_type>
struct _List_node<_Value_type, void *>
{ // list node
typedef _List_node<_Value_type, void *> *_Nodeptr;
_Nodeptr _Next; // successor node, or first element if head
_Nodeptr _Prev; // predecessor node, or last element if head
_Value_type _Myval; // the stored value, unused if head private:
_List_node& operator=(const _List_node&);
};
结点类包含前向指针和后向指针,双向链表,并且把赋值运算符的重载置为private,禁止了结点间进行赋值。
因为进行赋值如果是简单的引用传递,没有意义,如果新建了个一模一样的结点,链表就不再是链表,而形成了闭合的图结构。
至于它的基类_Simple_types<_Ty>则是一些基本类型的集合
// TEMPLATE CLASS _Simple_types
template<class _Value_type>
struct _Simple_types
{ // wraps types needed by iterators
typedef _Value_type value_type;
typedef size_t size_type;
typedef ptrdiff_t difference_type;
typedef value_type *pointer;
typedef const value_type *const_pointer;
typedef value_type& reference;
typedef const value_type& const_reference;
};
比如_Ty为int的话,_Simple_types里面的类型别名就是
int(值类型)、size_t(尺寸类型)、ptrdiff_t(差数类型)、int*(指针)、const int*(常指针)、int&(引用)、const int&(常饮用)
用了一些通用的接口来实现类型的统一。
【STL源码学习】std::list类的类型别名分析的更多相关文章
- stl源码学习(版本2.91)--list
stl源码学习(版本2.91)--list 一,阅读list()构造函数的收获 1,默认构造函数的作用和被调用的时机 struct no{ no(int i){} //no(){ // std::co ...
- 【STL源码学习】STL算法学习之二
第一章:前言 学习笔记,记录学习STL算法的一些个人所得,在以后想用的时候可以快速拾起. 第二章:明细 copy 函数原型: template <class InputIterator, cla ...
- 【STL源码学习】细品vector
第一节:vector简介 vector是一种典型的类模板,使用的时候必须进行实例化. vector的数据存储在数组上,支持随机访问迭代器,支持下标操作[]和at操作,支持手动扩容和自动容量增长. ve ...
- Mono源码学习笔记:Console类(四)
NullStream 类 (internal class) 以下就是 mcs/class/corlib/System.IO/NullStream.cs: 01: namespace System.IO ...
- STL源码学习----lower_bound和upper_bound算法
转自:http://www.cnblogs.com/cobbliu/archive/2012/05/21/2512249.html 先贴一下自己的二分代码: #include <cstdio&g ...
- STL源码学习----lower_bound和upper_bound算法[转]
STL中的每个算法都非常精妙,接下来的几天我想集中学习一下STL中的算法. ForwardIter lower_bound(ForwardIter first, ForwardIter last,co ...
- 【STL源码学习】STL算法学习之三
第一章:前言 数量不多,用到的时候会很爽. 第二章:明细 STL算法中的又一个分类:分割:将已有元素按照既定规则分割成两部分. is_partitioned 函数原型: template <c ...
- 【STL源码学习】STL算法学习之一
第一章:引子 STL包含的算法头文件有三个:<algorithm><numeric><functional>,其中最大最常用的是<algorithm>, ...
- [Android FrameWork 6.0源码学习] Window窗口类分析
了解这一章节,需要先了解LayoutInflater这个工具类,我以前分析过:http://www.cnblogs.com/kezhuang/p/6978783.html Window是Activit ...
随机推荐
- 前台cookie的操作
<html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> &l ...
- Hibernate主键生成策略详解
转载自:http://blog.csdn.net/wanghuan203/article/details/7562395 hibernate提供的主键生成策略,使我们可以在实体类的映射xml文件中设定 ...
- recovery.conf文件详解
在恢复过程中,用户可以通过使用recovery.conf文件来指定恢复的各个参数,如下: 归档恢复设置 restore_command:用于获取一个已归档段的XLOG日志文件的命令 archive_c ...
- d3.js(v5.7)的node与数据匹配(自动匹配扩展函数)
在d3操作时,当然少不了对已有节点绑定数据,那么问题就来了,节点个数和数据长度不一样的,怎么办. d3在节点少于数据长度的时候,有enter().appen()方法实现node的增加: 在节点大于数据 ...
- EventUtil对象
var EventUtil = { addHandler : function(element,type,handler){ if(element.addEventListener){ element ...
- flowable EngineConfiguration的作用和继承关系(1)
EngineConfiguration 是flowable引擎的核心部件. 在 flowable 中,实现引擎配置的顶层类是 AbstractEngineConfiguration 这是一个抽象类. ...
- Linux文件权限总结
一.用户组和用户 Linux是一个多用户.多任务环境,如下图,GroupA代表一个用户组,GroupB代表一个用户组,root是超级用户. Linux中,任何一个文件都有User.Group和othe ...
- java.io.IOException: Unable to establish loopback connection
1.错误描述 Starting preview server on port 8080 Modules: HTML5 (/HTML5) 2017-06-17 11:13:04.823:INFO::ma ...
- EasyDSS流媒体服务器Linux emerg getpwnam("xxx") failed解决办法
本文转自EasyDarwin开源团队Alex的博客:http://blog.csdn.net/cai6811376/article/details/73770943 EasyDSS 流媒体服务器是什么 ...
- Softmax 函数的特点和作用
---------- 因为这里不太方便编辑公式,所以很多公式推导的细节都已经略去了,如果对相关数学表述感兴趣的话,请戳这里的链接Softmax的理解与应用 - superCally的专栏 - 博客频道 ...