【2014-06-18】C++ STL读书笔记:stl_construct.h

发布时间:2026/9/25 18:21:43

【2014-06-18】C++ STL读书笔记:stl_construct.h [历史归档]本文原发布于 cstriker1407.info 个人博客内容为历史存档仅供参考。发布时间2014-06-18 标题C STL读书笔记stl_construct.h分类编程 / C C / C STL 标签CC·stl·constructC STL读书笔记stl_construct.h备注stl\_construct.hnew备注本读书笔记基于侯捷先生的《STL源码剖析》截图和注释版权均属于原作者所有。本读书笔记中的源码部分直接拷贝自SGI-STL部分代码删除了头部的版权注释但代码版权属于原作者。小弟初看stl很多代码都不是太懂注释可能有很多错误还请路过的各位大牛多多给予指导。stl_construct.h主要用于封装各种构造与析构函数部分源码如下为了节省版面删除了头部的版权注释#ifndef_STL_CONSTRUCT_H#define_STL_CONSTRUCT_H1//这里包含了new后面便可以使用placement newnew文件源码后面附上。#includenew#includebits/move.h//在std命名空间中_GLIBCXX_BEGIN_NAMESPACE(std)/** * Constructs an object in existing memory by invoking an allocated * objects constructor with an initializer. *///新实例的内存已经申请好了这里调用构造函数将其构造好。templatetypename_T1,typename_T2inlinevoid#ifdef__GXX_EXPERIMENTAL_CXX0X__// Allow perfect forwarding_Construct(_T1*__p,_T2__value)#else_Construct(_T1*__p,const_T2__value)#endif{// _GLIBCXX_RESOLVE_LIB_DEFECTS// 402. wrong new expression in allocator::construct::new(static_castvoid*(__p))_T1(_GLIBCXX_FORWARD(_T2,__value));//placement new可简单的理解为 _T1(__value);}/** * Destroy the object pointed to by a pointer type. *///析构单个实例//只有一个入参直接调用实例的析构函数。templatetypename_Tpinlinevoid_Destroy(_Tp*__pointer){__pointer-~_Tp();}//有两个参数分别是头尾部[)的iterator,通过iterator的累加分别调用一个入参的_Destroy函数进行析构//此版本的_Destroy_aux是需要【显式的】调用析构函数的。templateboolstruct_Destroy_aux{templatetypename_ForwardIteratorstaticvoid//这里使用的是static void这样便可以直接通过类名来调用了_Destroy_aux::,非常方便__destroy(_ForwardIterator __first,_ForwardIterator __last){for(;__first!__last;__first)std::_Destroy(*__first);}};//同上//此版本的_Destroy_aux是【不需要】调用析构函数的。个人的理解是如果实例比较简单不需要显示的调用析构函数//那么可以进行流程的优化省略掉调用析构函数的流程提高运行效率。templatestruct_Destroy_auxtrue{templatetypename_ForwardIteratorstaticvoid__destroy(_ForwardIterator,_ForwardIterator){}};/** * Destroy a range of objects. If the value_type of the object has * a trivial destructor, the compiler should optimize all of this * away, otherwise the objects destructors must be invoked. *///析构多个实例2参数版本//这里首先对是否需要调用析构函数进行判断如果实例比较简单比如没有动态内存的申请析构时无需显示调用析构函数比如类定义时析构函数就未实现。//就可以进行代码优化调用_Destroy_aux的不同版本。templatetypename_ForwardIteratorinlinevoid_Destroy(_ForwardIterator __first,_ForwardIterator __last){typedeftypenameiterator_traits_ForwardIterator::value_type _Value_type;std::_Destroy_aux__has_trivial_destructor(_Value_type)::__destroy(__first,__last);}/** * Destroy a range of objects using the supplied allocator. For * nondefault allocators we do not optimize away invocation of * destroy() even if _Tp has a trivial destructor. *///如果实例不是通过默认的allocator,而是通过其他的自定义的allocator来申请的那么在析构时就不能调用默认的析构函数了//就必须使用自定义的allocator的析构函数上面的注释也提到了这一点。templatetypename_Tpclassallocator;templatetypename_ForwardIterator,typename_Allocatorvoid_Destroy(_ForwardIterator __first,_ForwardIterator __last,_Allocator__alloc){for(;__first!__last;__first)__alloc.destroy(*__first);}//如果调用模板函数时显式的提到了allocator但不是自定义版本的那么底层的实现依然是默认的allocator。//直接调用2参数版本。templatetypename_ForwardIterator,typename_Tpinlinevoid_Destroy(_ForwardIterator __first,_ForwardIterator __last,allocator_Tp){_Destroy(__first,__last);}_GLIBCXX_END_NAMESPACE#endif/* _STL_CONSTRUCT_H */new文件部分源码如下为了节省版面删除了头部的版权注释#ifndef_NEW#define_NEW#pragmaGCC system_header#includecstddef#includeexception#pragmaGCC visibilitypush(default)externC{namespacestd{/** * brief Exception possibly thrown by c new. * ingroup exceptions * * c bad_alloc (or classes derived from it) is used to report allocation * errors from the throwing forms of c new. *///声明了一个【 bad_alloc 】异常类用来在new/delete失败时抛出class_GLIBCXX_IMPORTbad_alloc:publicexception{public:bad_alloc()throw(){}// This declaration is not useless:// http://gcc.gnu.org/onlinedocs/gcc-3.0.2/gcc_6.html#SEC118virtual~bad_alloc()throw();// See comment in eh_exception.cc.virtualconstchar*what()constthrow();};structnothrow_t{};externconstnothrow_t nothrow;/** If you write your own error handler to be called by c new, it must * be of this type. */typedefvoid(*new_handler)();/// Takes a replacement handler as the argument, returns the/// previous handler.new_handlerset_new_handler(new_handler)throw();}// namespace std//{/** These are replaceable signatures: * - normal single new and delete (no arguments, throw c bad_alloc on error) * - normal array new and delete (same) * - c nothrow single new and delete (take a c nothrow argument, return * c NULL on error) * - c nothrow array new and delete (same) * * Placement new and delete signatures (take a memory address argument, * does nothing) may not be replaced by a users program. *///各种重载 new/delete 运算符声明void*operatornew(std::size_t)throw(std::bad_alloc);void*operatornew[](std::size_t)throw(std::bad_alloc);voidoperatordelete(void*)throw();voidoperatordelete[](void*)throw();void*operatornew(std::size_t,conststd::nothrow_t)throw();void*operatornew[](std::size_t,conststd::nothrow_t)throw();voidoperatordelete(void*,conststd::nothrow_t)throw();voidoperatordelete[](void*,conststd::nothrow_t)throw();//Placement new 和 Placement delete 函数实现的非常简单// Default placement versions of operator new.inlinevoid*operatornew(std::size_t,void*__p)throw(){return__p;}inlinevoid*operatornew[](std::size_t,void*__p)throw(){return__p;}// Default placement versions of operator delete.inlinevoidoperatordelete(void*,void*)throw(){}inlinevoidoperatordelete[](void*,void*)throw(){}//}}// extern C#pragmaGCC visibility pop#endif
延伸阅读

更多相关文章

2026/9/23 17:47:51

从Arduino到STM32:PID控制与FreeRTOS实战指南

你第一次接触单片机,是不是也和我一样,面对琳琅满目的开发板、复杂的电路图和天书般的代码,感觉无从下手?想做个智能小车或者温控系统,却不知道从哪块板子开始,更别提什么PID、RTOS这些听起来就很高深的概念…

2026/9/19 23:05:11

Spring Boot配置模块化实战:多环境管理与工程实践

1. 从一次配置混乱引发的思考最近在排查一个线上服务的问题时,发现了一个挺有意思的现象:一个微服务模块的application.yml里,数据库连接配置和缓存配置混在一起,足足有几百行。更头疼的是,不同环境的配置(…

2026/9/25 10:34:03

PWM技术详解:从基础原理到STM32/Arduino实战应用

1. 从“开关”到“魔法”:PWM究竟是什么? 如果你玩过Arduino控制舵机,或者调过电脑风扇的转速,那你大概率已经和PWM打过交道了。PWM,全称脉冲宽度调制,听起来挺唬人,但它的核心思想其实特别简单…

2026/9/25 18:18:22

智能楼宇温湿度监测的以太网与Modbus TCP实战指南

1. 项目概述:为什么一栋楼的温湿度数据,值得花三个月重新搭一套网络?智能楼宇环境监测这事,表面看就是把几十个温湿度传感器装上去、连上网、看个数字——但真干过的人知道,这活儿干得糙,轻则数据三天两头断…

2026/9/25 18:18:22

城市的标志

城市标志01北京:天安门、故宫 02天津:天塔 03上海:上海东方明珠塔、金茂大厦 04重庆:解放碑 05河北石家庄:电视塔 06山西太原:永祚寺双塔、晋祠 07内蒙古呼和浩特:金刚座舍利宝塔 08辽宁沈阳&am…

2026/9/25 18:18:22

Ubuntu 24.04 安装 ToDesk 失败原因与 X11/Wayland 适配方案

1. 为什么在 Ubuntu 24.04 上装 ToDesk 不是“点几下就完事”的事?Ubuntu 24.04 LTS(Noble Numbat)发布后,大量用户发现——过去在 22.04 或 20.04 上顺滑安装的 ToDesk,这次卡在了第一步:双击.deb包提示“…

2026/9/25 18:18:22

希腊字母表

大写小写音标汉Αα/lfə/alpha阿尔法Ββ/bi:tə/ 或 /beɪtə/beta贝塔Γγ/gmə/gamma伽马Δδ/deltə/delta德尔塔Εε,ϵ/epsɪlɒn/epsilon艾普西隆Ζζ/zi:tə/zeta泽塔Ηη/i:tə/eta伊塔Θθ/θi:tə/theta西塔Ιι/aɪəʊtə/iota约(yāo)塔Κκ/kpə/kappa卡帕Λλ…

2026/9/24 20:24:47

GAMP 5 基于风险的计算机化系统验证:软件分类与审计追踪实践

简介:《A Risk-Based Approach to Compliant GxP Computerized Systems》即业内熟知的GAMP 5指南,面向制药企业质量与IT合规人员、验证工程师及计算机化系统管理者,用于解决GxP法规环境下系统合规性难以科学落地的问题。文档以风险管理为主线…

2026/9/23 12:06:55

安全托管MSSP实战:从静态防御到人机协同的攻防运营与应急响应

简介:这份PPT围绕互联网业务安全托管服务展开,面向企业安全负责人、IT运维人员及关注MSSP/MSS选型的读者,重点回应传统安全过度依赖人工、碎片化静态防御难以对抗产业化攻击等痛点。资源共1个pptx文件,包体约30.63MB,以…

2026/9/25 0:02:35

AI元人文:从工具使用到思维重构的深度探索

最近半年我一直在琢磨一件事:AI元人文到底是什么?说白了,就是“用元视角重新审视人与AI的关系”,也在“探索AI如何反向逼着我们发现自己的思考边界”。标题里的“元探索”,在我看就是一层套一层的追问——当你用AI解决…

2026/9/25 0:02:35

Python+CNN车牌识别实战:从数据预处理到模型训练与部署

简介:基于Python与卷积神经网络的车牌识别项目,面向计算机视觉初学者及智能交通开发者,目标是帮助用户掌握从数据预处理、模型构建到实际部署的完整流程。压缩包共25个文件,包含jpg/png图像样本、py训练脚本、md说明文档、dat数据…

2026/9/25 0:02:35

Vim基础操作全攻略:保存退出、模式切换与高频命令实战

1. 项目概述1.1 核心需求解析今天聊聊Vim。写这个题目的原因是:几乎每个后端开发者、运维人员、数据工程师某天都会遇到一个场景——深夜加班,服务器登录界面只有黑底白字,编辑器只有vi/vim,你必须在五分钟内完成一次配置修改并保…

2026/9/22 16:34:32

USB Type-C PCB布局分区设计:电源、高速信号与PD协议全攻略

做硬件这行,Type-C接口算是典型的“看着简单,做起来全坑”的东西。光引脚就24个,高低速信号、电源、控制线全部塞在一个小小的连接器里,如果PCB布局不做规划,打样回来基本就是“插上没反应”、“高速掉线”、“静电一打…

2026/9/22 20:01:30

系统编程学习原型如何补齐稳定性边界

系统编程学习原型如何补齐稳定性边界预算有限时&#xff0c;我先优化明显多余的复制&#xff0c;而不是猜测性地换容器。用借用传递只读数据通常就能减少分配&#xff1a; fn parse(line: &str) -> Result<Item, Error> { /* ... */ }用基准确认热点确实在分配&am…

2026/9/22 13:25:41

雨花区哪家财务公司代理记账比较好?

在雨花区&#xff0c;企业处理财税事务常常面临诸多挑战&#xff0c;选择一家靠谱的财务公司至关重要。湖南巨勤财务管理咨询有限公司就是本地正规实体财税服务机构&#xff0c;深耕本地工商财税行业多年&#xff0c;熟悉当地工商局、税务局最新政策与申报流程。主营公司注册、…

还想了解更多?直接咨询顾问

免费诊断 + 免费方案 + 透明报价。

全国咨询热线400-8866-253
免费获取方案
☎咨询二维码 ☎ ↑