【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 ...
随机推荐
- 前端开发必备 - Emmet
介绍 Emmet (前身为 Zen Coding) 是一个能大幅度提高前端开发效率的一个工具. 基本上,大多数的文本编辑器都会允许你存储和重用一些代码块,我们称之为"片段".虽然片 ...
- 关于px em rem的一点小总结
2015-11-28 06:06:40 概念 都是CSS单位. px:像素 Pixel.像素 (计算机屏幕上的一个点) em:1em 等于当前的字体尺寸. 2em 等于当前字体尺寸的两倍. 例如,如果 ...
- 如何让VS2013编写的程序在xp下运行
总体分c++程序和c#程序 1.c++程序 这个用C++编写的程序可以经过设置后在XP下运行,主要的“平台工具集”里修改就可以. 额外说明:(1)程序必须为Dotnet 4.0及以下版本.(XP只支持 ...
- 2017.10.23 Arduino Atmel EFM32低功耗监测
mark:因为最近一直调试(维护)客户的EFM32,和其他系列的Atmel芯片.对atmel不熟悉,做点功课. 1. 自建Arduino 最小系统(原来树莓派都是Atmel的芯片) http://ww ...
- JSP的EL和JSTL解析
1. EL 简介EL 全名为Expression Language,所有EL都是以${ 为起始.以} 为结尾的.EL 语法很简单,它最大的特点就是使用上很方便. 接下来介绍EL 主要的语法结构: ${ ...
- Linux:减号(-)详解
减号(-) 代表标准输出/标准输入, 视命令而定. “-”代替stdin和stdout的用法 为应用程序指定参数 ps -aux tar -zxf test.tar 一个减号和两个减号 一个减号后面跟 ...
- react 生命周期图
1. react v16 版本生命周期 2. react v17 (还未发布), 生命周期将被改动,下面红框的部分就是会被删除的部分,注意调整!
- Okhttp之CacheInterceptor简单分析
<OkHttp之BridgeInterceptor简单分析 >简单分析了BridgeInterceptor的工作原理,在Okhttp的拦截器链上BridgeInterceptor的下一个拦 ...
- 深度学习(六十七)metal forge深度学习库使用
1.设置输入: let input = Input() 或者 let input = Input(width: 100, height: 100, channels: 3) 2.创建网络: let o ...
- 【排序】归并排序,C++实现
原创文章,转载请注明出处! 博客文章索引地址 博客文章中代码的github地址 # 基本思想(分治法) 归并排序中, “归”代表递归的意思,即递归的将数组通过折半的方式分离为单个数组. “ ...