Skip to content

C++ 内部原理 - 完整文章索引

本栏目基于 GCC (libstdc++) 和 LLVM (libc++) 的源码,深入分析 C++ 语言和标准库的底层实现机制。共 63 篇文章,覆盖 C++ 的方方面面。


一、运行时与 ABI(6 篇)

#文章路径核心内容
1对象模型与内存布局runtime/object-model.mdvptr 位置、EBO、Itanium ABI 布局规则
2虚函数表实现runtime/vtable.mdvtable 结构、thunk 机制、VTT、虚析构双槽位
3RTTI 与 dynamic_castruntime/rtti.mdtype_info、dynamic_cast 算法、-fno-rtti
4异常处理 ABIruntime/exception-handling.md.eh_frame、LSDA、栈展开、__cxa_throw
5名称修饰runtime/name-mangling.mdItanium ABI 编码、MSVC 编码、c++filt
6调用约定runtime/calling-conventions.mdSysV AMD64、MSVC x64、结构体传递

二、容器实现(11 篇)

#文章路径核心内容
7std::vectorcontainers/vector.md三指针布局、扩容策略、vector<bool> 特化
8std::stringcontainers/string.mdSSO、COW、双 ABI、_M_mutate
9std::listcontainers/list.md双向链表、哨兵节点、splice
10std::forward_listcontainers/forward-list.md单向链表、before_begin、insert_after
11std::dequecontainers/deque.md中控器、缓冲区、迭代器设计
12std::map/setcontainers/map-set.md红黑树、旋转算法、迭代器递增
13std::unordered_mapcontainers/unordered-map.md哈希表、链地址法、rehash 策略
14std::arraycontainers/array.md固定大小数组、constexpr 支持
15std::stack/queuecontainers/stack-queue.md容器适配器、底层容器选择
16std::flat_mapcontainers/flat-map.md排序 vector、二分查找、C++23
17std::inplace_vectorcontainers/inplace-vector.md栈上固定容量、C++26

三、智能指针与内存(7 篇)

#文章路径核心内容
18std::unique_ptrmemory/unique-ptr.md压缩指针、删除器存储、移动语义控制
19std::shared_ptrmemory/shared-ptr.md控制块、原子引用计数、双字优化
20std::weak_ptrmemory/weak-ptr.md弱引用计数、lock() 机制、内存序
21分配器模型memory/allocator.mdstd::allocator、allocator_traits、EBO
22PMR 多态内存资源memory/pmr.mdmemory_resource、polymorphic_allocator
23GNU 扩展分配器memory/ext-allocators.mdpool/bitmap/mt/malloc 分配器
24内存操作基础设施memory/operations.mduninitialized_copy/fill、construct_at

四、工具类实现(7 篇)

#文章路径核心内容
25std::functionutilities/function.md类型擦除、SBO、管理器操作
26std::variantutilities/variant.md标签联合、visit、jump table
27std::anyutilities/any.md类型擦除、小/大对象策略
28std::optionalutilities/optional.md存储布局、constexpr、value_or
29std::tupleutilities/tuple.md递归继承、get、tuple_size
30std::pairutilities/pair.mdEBO、piecewise_construct、三向比较
31std::bitsetutilities/bitset.md位运算、popcount、to_string

五、类型系统与元编程(5 篇)

#文章路径核心内容
32Type Traitstemplates/type-traits.mdintegral_constant、enable_if、void_t
33SFINAEtemplates/sfinae.mdSFINAE 上下文、检测惯用法
34Conceptstemplates/concepts.mdrequires 表达式、subsumption
35constexprtemplates/constexpr.md编译期求值、consteval、constinit
36模板实例化templates/instantiation.md两阶段查找、显式实例化、偏特化

六、算法与迭代器(4 篇)

#文章路径核心内容
37迭代器体系algorithms/iterators.mditerator_traits、迭代器标签、指针特化
38排序算法algorithms/sorting.mdIntroSort、三数取中、分区算法
39Rangesalgorithms/ranges.mdview_interface、管道运算符、惰性求值
40并行算法algorithms/parallel.md执行策略、TBB/OpenMP 后端、SIMD

七、并发原语(6 篇)

#文章路径核心内容
41std::atomicconcurrency/atomic.md原子操作内建函数、memory_order、atomic_flag
42std::threadconcurrency/thread.mdpthread 封装、join/detach、this_thread
43std::mutexconcurrency/mutex.mdlock_guard、unique_lock、RAII
44std::condition_variableconcurrency/condition-variable.mdwait/notify、虚假唤醒、cv_status
45std::future/promiseconcurrency/future.mdshared_state、原子操作、条件变量
46std::jthreadconcurrency/jthread.mdstop_token、stop_callback、协作式取消

八、I/O 与格式化(3 篇)

#文章路径核心内容
47std::formatio/format.md编译期解析、formatter、format_context
48std::printio/print.md缓冲输出、FILE*、fwrite_unlocked
49流类体系io/streams.mdbasic_ostream、streambuf、缓冲区管理

九、C++20/23 新特性(6 篇)

#文章路径核心内容
50协程 Loweringcpp20/coroutines.md协程帧、promise_type、suspend_always
51Modulescpp20/modules.mdBMI、模块分区、头单元
52std::spancpp20/span.md非拥有视图、dynamic_extent、subspan
53std::expectedcpp23/expected.mdunexpected、transform、and_then
54std::generatorcpp23/generator.mdco_yield、惰性求值、迭代器
55std::mdspancpp23/mdspan.mdextents、layout_right/left、多维访问

十、编译器内部(4 篇)

#文章路径核心内容
56GCC 编译器内部compiler/gcc-internals.mdGENERIC/GIMPLE/RTL、C++ 前端
57Clang/LLVM 编译器内部compiler/clang-internals.mdClang AST、LLVM IR、代码生成
58去虚拟化compiler/devirtualization.md类型传播、LTO、final 优化
59编译器优化管线compiler/optimization-pipeline.md内联、循环优化、向量化

十一、标准库综合对比(4 篇)

#文章路径核心内容
60容器对比comparison/containers.mdvector/string/map 等布局与实现差异
61智能指针对比comparison/smart-pointers.md控制块、make_shared、ABI 兼容性
62字符串 ABI 对比comparison/string-abi.md__cxx11 双 ABI、__1 内联命名空间
63并发实现对比comparison/concurrency.mdatomic/mutex/thread 实现差异

统计

分类篇数已增强源码分析
运行时与 ABI66/6 ✅
容器实现1111/11 ✅
智能指针与内存77/7 ✅
工具类实现77/7 ✅
类型系统与元编程55/5 ✅
算法与迭代器44/4 ✅
并发原语66/6 ✅
I/O 与格式化33/3 ✅
C++20/23 新特性66/6 ✅
编译器内部44/4 ✅
标准库综合对比44/4 ✅
合计6363/63

源码参考

组件GCC (libstdc++)LLVM (libc++)
vectorbits/stl_vector.h, bits/vector.tcc__vector/vector.h
stringbits/basic_string.h, bits/basic_string.tccstring
shared_ptrbits/shared_ptr_base.h__memory/shared_ptr.h
unique_ptrbits/unique_ptr.h__memory/unique_ptr.h
allocatorbits/allocator.h, bits/alloc_traits.h__memory/allocator.h
map/setbits/stl_tree.h__tree
unordered_mapbits/hashtable.h__hash_table
atomicbits/atomic_base.h__atomic/atomic.h
mutexbits/std_mutex.h__mutex/mutex.h
threadbits/std_thread.h__thread/thread.h
formatbits/formatfwd.h__format/
rangesbits/ranges_base.h__ranges/
coroutinescoroutine__coroutine/

基于 MIT 许可发布