语言支持库
第 17 章导读:核心语言在背后隐式调用的函数、隐式生成的对象的类型都定义在这一章,例如 operator new、type_info、initializer_list 和比较类别类型。
本节要点
- 读完这一章,可以回答这样的问题:
new T实际调用了哪个函数?为什么用<=>之前必须包含<compare>?std::exit会析构哪些对象? - 这一章的组件被核心语言条款直接引用,相当于定义在头文件里的语言特性。
- 有些语言特性要求先包含对应的头文件,否则程序非良构;全局
operator new则无需包含任何头文件就已声明,而且可以被程序替换。 - 这一章的头文件大多要求独立实现也提供,因为没有它们,相应的语言特性就无法使用。
语言在背后调用的库#
¶1 第 17 章 [support] 描述某些 C++ 程序执行时被隐式调用的函数、隐式生成的对象的类型,以及声明它们的头文件。
隐式是这里的关键。程序没有写 operator new(sizeof(T)),但 new T 调用了它;没有写 throw std::bad_cast(),但失败的 dynamic_cast<T&> 抛出了它;没有构造 std::initializer_list,但 {1, 2, 3} 在某些语境下生成了它;没有提到 std::coroutine_traits,但每个协程都要靠它找到 promise 类型。核心语言在这些地方直接点名使用库组件,所以这些组件实际上是语言的一部分。
本章地图#
¶2 表 42 列出了各子条款及其头文件:
| 子条款 | 头文件 | 与核心语言的联系 |
|---|---|---|
| [support.types] 公共定义 | <cstddef>、<cstdlib> |
sizeof 的类型 size_t,指针相减的类型 ptrdiff_t,nullptr 的类型 nullptr_t,std::byte |
| [support.limits] 实现属性 | <cfloat>、<climits>、<limits>、<version> |
查询 [basic.fundamental] 中由实现决定的性质;库特性测试宏 |
| [support.arith.types] 算术类型 | <cstdint>、<stdfloat> |
定宽整数;扩展浮点类型的别名([basic.extended.fp]) |
| [support.start.term] 启动与终止 | <cstdlib> |
从 main 返回等于调用 exit([basic.start.main]);atexit 与静态对象析构的顺序([basic.start.term]) |
| [support.dynamic] 动态内存管理 | <new> |
new 表达式和 delete 表达式调用的分配与释放函数([new.delete])、bad_alloc、std::launder |
| [support.rtti] 类型识别 | <typeinfo>、<typeindex> |
typeid 的结果 type_info;bad_cast、bad_typeid |
| [support.srcloc] 源码位置 | <source_location> |
调用点的文件名和行号 |
| [support.exception] 异常处理 | <exception> |
std::terminate([except.terminate])、uncaught_exceptions、exception_ptr |
| [support.contract] 契约违反处理 | <contracts> |
传给契约违反处理函数的 contract_violation([basic.contract]) |
| [support.initlist] 初始化列表 | <initializer_list> |
列表初始化生成的 initializer_list(<a class="xref std local" href="../std/dcl.init.list.html" data-tip="9.5.5 List-initialization · 列表初始化 — 花括号初始化怎样选择初始化方式:¶3 的十二步判定、initializer-list 构造函数为什么总是优先、backing array 的类型与寿命、窄化转换的完整定义,以及 auto 和 CTAD 遇到花括号时的特殊规则。">[dcl.init.list]) |
| [cmp] 比较 | <compare> |
<=> 的结果类型([expr.spaceship]) |
| [support.coroutine] 协程 | <coroutine> |
promise 类型的查找、协程句柄([dcl.fct.def.coroutine]) |
| [support.runtime] 其他运行时支持 | <csetjmp>、<csignal>、<cstdarg>、<cstdlib> |
可变参数([expr.call])、longjmp、信号处理函数中能做什么 |
| [support.c.headers] C 头文件 | <stdio.h> 等 |
与 C 代码互操作 |
语言与库的三种耦合方式#
必须先包含头文件。 使用 typeid 之前要包含 <typeinfo>([expr.typeid]),使用 <=> 之前要包含 <compare>([expr.spaceship]),列表初始化用到 std::initializer_list 时它必须已经声明(<a class="xref std local" href="../std/dcl.init.list.html" data-tip="9.5.5 List-initialization · 列表初始化 — 花括号初始化怎样选择初始化方式:¶3 的十二步判定、initializer-list 构造函数为什么总是优先、backing array 的类型与寿命、窄化转换的完整定义,以及 auto 和 CTAD 遇到花括号时的特殊规则。">[dcl.init.list]),协程需要 std::coroutine_traits([dcl.fct.def.coroutine])。缺少头文件时程序非良构。
struct P {
int x;
auto operator<=>(const P&) const
= default;
};
// error: 没有包含 <compare>#include <compare>
struct P {
int x;
auto operator<=>(const P&) const
= default;
};隐式声明,可以替换。 全局 operator new 和 operator delete 在每个翻译单元中都已隐式声明,不需要包含 <new>。程序可以提供自己的版本替换它们([dcl.fct.def.replace]),但替换版本必须满足 [new.delete] 的要求。契约违反处理函数能否替换由实现决定([basic.contract.handler])。
形式上是库,实际由编译器实现。 offsetof、std::launder、std::source_location::current、coroutine_handle 的各个操作都无法用普通的 C++ 代码写出来。Clang 用内建函数实现它们,例如 __builtin_offsetof、__builtin_launder。
读库条款要分清的几种要求#
库条款用一套固定的元素描述每个函数([structure.specifications])。它们的后果不同,读这一章时要随时分辨:
| 元素 | 不满足时的后果 | 本章的例子 |
|---|---|---|
| Mandates | 程序非良构,必须诊断 | std::launder<T> 要求 T 不是函数类型或 void |
| Constraints | 这个声明不参与重载决议 | std::byte 的移位运算要求移位量是整数类型 |
| Preconditions | 未定义行为 | coroutine_handle::resume 要求协程处于挂起状态,且不在最终挂起点 |
| Required behavior | 对替换函数和处理函数的要求,违反则行为未定义 | 替换的 operator new 失败时必须抛 bad_alloc;terminate_handler 不得返回 |
Effects 中写 Equivalent to 时,规定的语义就是那段代码的语义,连其中的未定义行为一起照搬。
独立实现#
独立实现(freestanding implementation)是可以在没有操作系统的环境中运行的实现,常见于内核和嵌入式开发,它只需提供标准库的一个子集([compliance])。这一章的 <cstddef>、<limits>、<new>、<typeinfo>、<exception>、<initializer_list>、<compare>、<coroutine>、<source_location>、<contracts>、<cstdarg> 等都在这个子集里,因为没有它们,new、typeid、<=>、协程这些语言特性就无法使用。
条款一览
- 17.1General 概述[support.general]
- 17.2Common definitions 公共定义[support.types]完成
- 17.3Implementation properties 实现属性[support.limits]完成
- 17.3.1General 概述[support.limits.general]
- 17.3.2Header <version> synopsis 头文件 <version> 概要[version.syn]
- 17.3.3Header <limits> synopsis 头文件 <limits> 概要[limits.syn]
- 17.3.4Enum float_round_style 枚举 float_round_style[round.style]
- 17.3.5Class template numeric_limits 类模板 numeric_limits[numeric.limits]
- 17.3.6Header <climits> synopsis 头文件 <climits> 概要[climits.syn]
- 17.3.7Header <cfloat> synopsis 头文件 <cfloat> 概要[cfloat.syn]
- 17.4Arithmetic types 算术类型[support.arith.types]完成
- 17.5Startup and termination 启动与终止[support.start.term]完成
- 17.6Dynamic memory management 动态内存管理[support.dynamic]完成
- 17.6.1General 概述[support.dynamic.general]
- 17.6.2Header <new> synopsis 头文件 <new> 概要[new.syn]
- 17.6.3Storage allocation and deallocation 存储的分配与解分配[new.delete]完成
- 17.6.4Storage allocation errors 存储分配错误[alloc.errors]完成
- 17.6.5Pointer optimization barrier 指针优化屏障[ptr.launder]完成
- 17.6.6Hardware interference size 硬件干涉大小[hardware.interference]
- 17.7Type identification 类型识别(typeid)[support.rtti]完成
- 17.7.1General 概述[support.rtti.general]
- 17.7.2Header <typeinfo> synopsis 头文件 <typeinfo> 概要[typeinfo.syn]
- 17.7.3Class type_info 类 type_info[type.info]
- 17.7.4Class bad_cast 类 bad_cast[bad.cast]
- 17.7.5Class bad_typeid 类 bad_typeid[bad.typeid]
- 17.7.6Header <typeindex> synopsis 头文件 <typeindex> 概要[type.index.synopsis]
- 17.7.7Class type_index 类 type_index[type.index]完成
- 17.8Source location 源码位置[support.srcloc]完成
- 17.9Exception handling 异常处理[support.exception]完成
- 17.9.1General 概述[support.exception.general]
- 17.9.2Header <exception> synopsis 头文件 <exception> 概要[exception.syn]
- 17.9.3Class exception 类 exception[exception]
- 17.9.4Class bad_exception 类 bad_exception[bad.exception]
- 17.9.5Abnormal termination 异常终止[exception.terminate]
- 17.9.6uncaught_exceptions uncaught_exceptions[uncaught.exceptions]
- 17.9.7Exception propagation 异常传播[propagation]完成
- 17.9.8nested_exception nested_exception[except.nested]
- 17.10Contract-violation handling 契约违反处理[support.contract]完成
- 17.11Initializer lists 初始化器列表[support.initlist]完成
- 17.12Comparisons 比较[cmp]完成
- 17.12.1Header <compare> synopsis 头文件 <compare> 概要[compare.syn]
- 17.12.2Comparison category types 比较类别类型[cmp.categories]
- 17.12.3Class template common_comparison_category 类模板 common_comparison_category[cmp.common]
- 17.12.4Concept three_way_comparable 概念 three_way_comparable[cmp.concept]
- 17.12.5Result of three-way comparison 三路比较的结果[cmp.result]
- 17.12.6Comparison algorithms 比较算法[cmp.alg]
- 17.12.7Type Ordering 类型排序[compare.type]
- 17.13Coroutines 协程[support.coroutine]完成
- 17.13.1General 概述[support.coroutine.general]
- 17.13.2Header <coroutine> synopsis 头文件 <coroutine> 概要[coroutine.syn]
- 17.13.3Coroutine traits 协程特征[coroutine.traits]
- 17.13.4Class template coroutine_handle 类模板 coroutine_handle[coroutine.handle]
- 17.13.4.1General 概述[coroutine.handle.general]
- 17.13.4.2Construct/reset 构造/重置[coroutine.handle.con]
- 17.13.4.3Conversion 转换[coroutine.handle.conv]
- 17.13.4.4Export/import 导出/导入[coroutine.handle.export.import]
- 17.13.4.5Observers 观察器[coroutine.handle.observers]
- 17.13.4.6Resumption 恢复[coroutine.handle.resumption]
- 17.13.4.7Promise access promise 访问[coroutine.handle.promise]
- 17.13.4.8Comparison operators 比较运算符[coroutine.handle.compare]
- 17.13.4.9Hash support 散列支持[coroutine.handle.hash]
- 17.13.5No-op coroutines 无操作协程[coroutine.noop]
- 17.13.5.1Class noop_coroutine_promise 类 noop_coroutine_promise[coroutine.promise.noop]
- 17.13.5.2Class coroutine_handle<noop_coroutine_promise> 类 coroutine_handle<noop_coroutine_promise>[coroutine.handle.noop]
- 17.13.5.2.1General 概述[coroutine.handle.noop.general]
- 17.13.5.2.2Conversion 转换[coroutine.handle.noop.conv]
- 17.13.5.2.3Observers 观察器[coroutine.handle.noop.observers]
- 17.13.5.2.4Resumption 恢复[coroutine.handle.noop.resumption]
- 17.13.5.2.5Promise access promise 访问[coroutine.handle.noop.promise]
- 17.13.5.2.6Address 地址[coroutine.handle.noop.address]
- 17.13.5.3Function noop_coroutine 函数 noop_coroutine[coroutine.noop.coroutine]
- 17.13.6Trivial awaitables 平凡可等待体[coroutine.trivial.awaitables]
- 17.14Other runtime support 其他运行时支持[support.runtime]完成
- 17.15C headers C 头文件[support.c.headers]完成
- 17.15.1General 概述[support.c.headers.general]
- 17.15.2Header <complex.h> synopsis 头文件 <complex.h> 概要[complex.h.syn]
- 17.15.3Header <iso646.h> synopsis 头文件 <iso646.h> 概要[iso646.h.syn]
- 17.15.4Header <stdalign.h> synopsis 头文件 <stdalign.h> 概要[stdalign.h.syn]
- 17.15.5Header <stdbool.h> synopsis 头文件 <stdbool.h> 概要[stdbool.h.syn]
- 17.15.6Header <tgmath.h> synopsis 头文件 <tgmath.h> 概要[tgmath.h.syn]
- 17.15.7Other C headers 其他 C 头文件[support.c.headers.other]