zoukankan      html  css  js  c++  java
  • STL 源码分析 (SGI版本, 侯捷著)

    前言

    源码之前,了无秘密

    algorithm的重要性

    效率的重要性

    采用Cygnus C++ 2.91 for windows

      cygwin-b20.1-full2.exe

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

    安装后, 源码默认在:C:cygnuscygwin-b20includeg++

    第一章 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是个静态的函数。这个跟类内外没有关系。

  • 相关阅读:
    10个最佳jQuery Lightbox效果插件收集
    JavaScript 中的内存泄露模式
    推荐6 款免费的图标编辑器
    Google 排名中的 10 个最著名的 JavaScript 库
    影响搜索引擎排名的因素2009年(总览)
    2009 年度最佳 jQuery 插件
    使用 Nginx 提升网站访问速度
    10个新的最有前途的JavaScript框架
    IE8面向Web开发人员的功能改进
    IE6, IE7, IE8 CSS 兼容速查表
  • 原文地址:https://www.cnblogs.com/lpthread/p/3479482.html
Copyright © 2011-2022 走看看