您的位置:首页 > 其它

STL 源码分析 (SGI版本, 侯捷著)

2013-12-17 23:21 1306 查看

前言

源码之前,了无秘密

algorithm的重要性

效率的重要性

采用Cygnus C++ 2.91 for windows

  cygwin-b20.1-full2.exe

  下载地址:http://download.csdn.net/detail/ljljlj/3658798

安装后, 源码默认在:C:\cygnus\cygwin-b20\include\g++

第一章 STL概论

stl_config.h: 定义各个厂家编译器对C++特性的支持程度

__GNUC__ : 2

__GNUC_MINOR__ : 91

GNUC 2.91的能力

// (3) Defines __STL_STATIC_TEMPLATE_MEMBER_BUG if the compiler can't
// handle static members of template classes. 没有这个bug

// (5) Defines __STL_CLASS_PARTIAL_SPECIALIZATION if the compiler
// supports partial specialization of class templates.

// (6) Defines __STL_FUNCTION_TMPL_PARTIAL_ORDER if the compiler supports
// partial ordering of function templates (a.k.a partial specialization
// of function templates.

// (8) Defines __STL_MEMBER_TEMPLATES if the compiler supports
// template members of classes.

// (10) Defines __STL_LIMITED_DEFAULT_TEMPLATES if the compiler is
// unable to handle default template parameters that depend on
// previous template parameters.

// (11) Defines __STL_NON_TYPE_TMPL_PARAM_BUG if the compiler has
// trouble performing function template argument deduction for
// non-type template parameters.

__STL_NULL_TMPL_ARGS

__STL_TEMPLATE_NULL

临时对象
template<typename T>

class A{

void operator () (){

cout << "test" << endl

}

}

A()是一个临时对象, 不是函数调用

int(8)也是临时对象

仿函数
重载了 () 的对象时仿函数, 例如上面的A

函数指针无法保存局部状态, 也不能进行适配性adaptability

第二章 空间配置器

本章主要讲解内存分配和构造析构两部分知识。

其中构造析构利用template识别POD类型进行不同的操作, 以提高效率。

内存分配, 避免内存碎片和频繁申请小内存, 使用free_list保存16(8, 16 ... 128) 中不同大小的区块。

具体思想可以详细看第二章内容。

operator new

  ::operator new

placement new

  new(p) T(value) // invoke ctor

void (* __malloc_alloc_oom_handler) (); // 这行是定义一个函数指针

等同于:

void (*) () __malloc_alloc_oom_handler;

这个函数怎么读

static void (* set_malloc_handler(void (*f)()))()
{
void (* old)() = __malloc_alloc_oom_handler;
__malloc_alloc_oom_handler = f;
return(old);
}


从里向外读吧,就是一个无形参,返回类型为void的函数f的指针,做为set_malloc_handler的一个形参;

而这个set_malloc_handler是一个函数, 返回值为一个函数指针

而这个函数指针的定义是: 返回类型为void,且没有形参.

set_malloc_handler是个静态的函数。这个跟类内外没有关系。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: