发布时间:2026/8/7 3:42:10
【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/8/7 3:42:10

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

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

2026/8/7 3:42:10

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

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

2026/8/7 3:42:09

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

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

2026/8/7 5:42:17

SpringBoot+Vue高校学生辅助系统开发实践

1. 项目概述:高校学生辅助系统的核心价值高校学生辅助系统是数字化校园建设中的重要一环,它直接服务于教学管理和学生日常事务。传统的学生管理工作往往面临信息孤岛、流程繁琐、响应滞后等问题。比如选课冲突需要人工核对、请假审批要跑多个办公室、成绩…

2026/8/7 5:42:17

Java CompletableFuture 异步编程实战:从基础原理到高并发应用

1. 项目概述:为什么我们需要CompletableFuture?如果你写过Java并发代码,大概率被Future接口“折磨”过。想象一个场景:你需要调用三个独立的远程服务来组装一个页面数据,用传统的Future,你得一个个get()&am…

2026/8/7 5:42:17

C语言static关键字详解:从内存模型到模块化设计

1. 项目概述:为什么static是C语言里绕不开的“钉子户”?如果你写过C语言,哪怕只是写过“Hello, World”,大概率也见过static这个关键字。它就像代码世界里的一个“钉子户”,看着不起眼,但一旦你开始构建稍微…

2026/8/7 5:42:17

BMC SNMP配置与监控集成实战:从协议安全到Zabbix/Prometheus对接

1. 项目概述:为什么BMC的SNMP功能值得深挖?在数据中心和服务器运维的日常里,我们打交道最多的往往是操作系统和应用层。但真正决定一台服务器“健康”与“可控”状态的,其实是水面之下的基板管理控制器,也就是BMC。最近…

2026/8/7 5:37:17

LLM训练效率优化实战:缓存、通信重叠与MoE路由优化提升25%速度

1. 项目概述:一次关于LLM训练效率的深度探索最近在优化一个大型语言模型的训练流程时,我们团队成功将整体训练速度提升了约25%。这个数字听起来可能不算惊天动地,但在动辄消耗数百万美元计算资源、训练周期以月计的LLM领域,每一分…

2026/8/5 3:13:11

如何用免费工具突破游戏窗口限制:SRWE完整使用指南

如何用免费工具突破游戏窗口限制:SRWE完整使用指南 【免费下载链接】SRWE Simple Runtime Window Editor 项目地址: https://gitcode.com/gh_mirrors/sr/SRWE 你是否遇到过这样的困扰?想为心爱的游戏截图,却发现游戏不支持自定义分辨率…

2026/8/7 0:01:55

CAD图库管理:从文件归档到设计资产管理的效率革命

你肯定遇到过这种情况:打开一个老项目,想找某个特定的图块——比如一个标准的门、一个特定的设备符号,或者一个公司logo。你记得它就在某个DWG文件里,或者曾经从某个同事那里拷来过。于是,你开始在一堆命名混乱的文件夹…

2026/8/7 0:01:55

5分钟掌握Wand-Enhancer:2026年终极WeMod专业版免费解锁指南

5分钟掌握Wand-Enhancer:2026年终极WeMod专业版免费解锁指南 【免费下载链接】Wand-Enhancer Advanced UX and interoperability extension for Wand (WeMod) app 项目地址: https://gitcode.com/GitHub_Trending/we/Wand-Enhancer Wand-Enhancer是一款功能强…

2026/8/7 0:01:55

“Quality Control(质量控制)”在软件工程中通常指通过一系列活动确保软件产品符合预定的质量标准和用户需求

“Quality Control(质量控制)”在软件工程中通常指通过一系列活动确保软件产品符合预定的质量标准和用户需求。而“软件测试”是质量控制的关键手段之一,属于QC范畴下的具体实践,其目标是发现缺陷、验证功能正确性、评估软件质量属…

2026/8/5 19:21:13

实测才敢推 AI论文网站 2026最新测评与推荐

2026年真正好用的AI论文网站,核心看生成的论文质量、低AI味、格式正确、学术适配四大指标。综合实测,千笔AI、ThouPen、豆包、DeepSeek、Grammarly 是当前最值得推荐的梯队,覆盖从免费到付费、从中文到英文、从文科到理工的全场景需求。一、综…

2026/8/5 19:21:13

2026必备!AI论文网站测评:最新推荐与深度对比

2026年真正好用的AI论文网站,核心看生成的论文质量、低AI味、格式正确、学术适配四大指标。综合实测,千笔AI、ThouPen、豆包、DeepSeek、Grammarly 是当前最值得推荐的梯队,覆盖从免费到付费、从中文到英文、从文科到理工的全场景需求。 一、…

2026/8/6 20:45:01

摆脱论文困扰!盘点2026年全网爆红的的AI论文写作工具

一天写完毕业论文在2026年已不再是天方夜谭。2026年最炸裂、实测能大幅提速的AI论文写作工具,覆盖选题构思、文献整理、内容生成、格式排版等核心场景,真正帮你高效搞定论文难题。 一、全流程王者:一站式搞定论文全链路(一天定稿首…