C++ 第20D册 比例时间与补充设施 2003与2011正式版中文笔记
跳转到“C++ 第20D册 比例时间与补充设施 2003与2011正式版中文笔记”0 范围与阅读约定
跳转到“0 范围与阅读约定”本册依据用户提供的ISO_IEC 14882-2003.pdf与ISO IEC 14882 2011 (C++11).pdf,覆盖2003 20.5 Date and time(印刷381,PDF物理409)以及2011 20.10–20.13(印刷602–627,PDF物理618–643)。后者包括ratio、chrono、C日期时间、scoped_allocator_adaptor与type_index。本册不是按当代头文件接口倒推旧版本;例如没有2011之后的日历、时区、chrono字面量、round/floor/ceil或clock_cast。
审查状态:本册规定的两版范围已完成独立静态审查;第10节列出副本可见限制。各编号款及表按本版小节定位,中文为释义,声明重排,原例单列,原创例和图只作静态分析,示例未编译运行。
2003全源已知缺失页不落本范围。所给PDF副本如出现缺字或声明不一致,均需查看实际页图;页图只能证明该副本的可见内容,不能证明ISO最初出版物有相同排印错误。疑点不以现代资料静默补齐。
1 2003 20.5 日期时间
跳转到“1 2003 20.5 日期时间”1.1 20.5 [lib.date.time] 款1–2 表35
跳转到“1.1 20.5 [lib.date.time] 款1–2 表35”款1的<ctime>表35在所给PDF印刷381实际列出:
| 类别 | 名字 |
|---|---|
| 宏 | NULL |
| 类型 | size_t、clock_t、time_t |
| 结构 | tm |
| 函数 | asctime、clock、difftime、localtime、strftime、ctime、gmtime、mktime、time |
款2规定内容与标准C库<time.h>相同,交叉引用本版所引用ISO C的7.12及Amendment 1的4.6.4。本章没有重复逐函数完整C规范,所以不能把表中只有名字误当作C函数全部契约。表中宏栏仅见NULL,不据此否定引用进来的C头文件其他规定,也不在这个表内伪造未印出的条目。
职责导读:clock/clock_t表达处理器时间体系;time/time_t表达日历时间体系;difftime计算两个日历时间差;gmtime、localtime转为分解时间;mktime反向解释本地分解时间;asctime、ctime及strftime用于字符表示。这是名字角色的说明,具体失败值、缓冲、线程安全、格式与表示限制应回到被引用C条款。本段不把2003 time_t规定成Unix整数秒,也不将2011 chrono钟与它自动视为同一epoch。
2003没有本册后述std::chrono、std::ratio、scoped_allocator_adaptor和type_index的同名标准库章节;不能给这份2003笔记附上貌似同版的对应接口。
2 2011 20.10 编译期有理数
跳转到“2 2011 20.10 编译期有理数”2.1 20.10.1 [ratio.general] 款1–2 与20.10.2 [ratio.syn]
跳转到“2.1 20.10.1 [ratio.general] 款1–2 与20.10.2 [ratio.syn]”款1:ratio以intmax_t可表示的编译期分子/分母精确表达有限有理数。款2:本节把R1/R2作为ratio参数时,若它们不是ratio模板特化,程序不良构;“恰好有num/den两个同名成员”的用户类不自动满足。
<ratio>概览声明:template<intmax_t N,intmax_t D=1> class ratio;四个alias template ratio_add、ratio_subtract、ratio_multiply、ratio_divide;六个比较类模板ratio_equal、ratio_not_equal、ratio_less、ratio_less_equal、ratio_greater、ratio_greater_equal;以及下文20种SI前缀别名。
2.2 20.10.3 [ratio.ratio] 款1–2
跳转到“2.2 20.10.3 [ratio.ratio] 款1–2”template<intmax_t N, intmax_t D = 1>class ratio {public: typedef ratio<num,den> type; static constexpr intmax_t num; static constexpr intmax_t den;};款1:D不能为0,N和D各自的绝对值也须能由intmax_t表示,否则不良构。注解释这排除无穷比值;二进制补码下还排除绝对值无法表示的最负数。不只是检查输入本身能否放入有符号类型。
款2:令gcd为abs(N)、abs(D)的最大公约数,num = sign(N)*sign(D)*abs(N)/gcd,den = abs(D)/gcd。分母成为正数,公因数约去,符号留在分子。ratio<2,4>这个类模板特化的类型身份不因此变成ratio<1,2>;它的::type才命名规范化的ratio类型。
原创静态例:ratio<-6,-8>的num为3、den为4;ratio<0,7>的num为0、den为1。无需生成运行期分数对象来得到这些常量。
2.3 20.10.4 [ratio.arithmetic] 款1–3 表58
跳转到“2.3 20.10.4 [ratio.arithmetic] 款1–3 表58”款1:四个别名表示两个ratio的运算结果。先按表58的数学表达式得到X、Y,再归一化成U=ratio<X,Y>::num、V=ratio<X,Y>::den,别名命名ratio<U,V>。
| 别名 | X | Y |
|---|---|---|
ratio_add<R1,R2> | R1::num*R2::den + R2::num*R1::den | R1::den*R2::den |
ratio_subtract<R1,R2> | R1::num*R2::den - R2::num*R1::den | R1::den*R2::den |
ratio_multiply<R1,R2> | R1::num*R2::num | R1::den*R2::den |
ratio_divide<R1,R2> | R1::num*R2::den | R1::den*R2::num |
款2保留两层范围边界:最终U或V不能用intmax_t表示,程序不良构;若最终可表示,实现应给出正确值;中间X或Y不能表示时,除非实现仍算出正确U/V,否则程序不良构。不能只检查最终约分后数值就宣称所有实现必须接受;也不能把数学表式直接当成一定先执行会溢出的机器整数乘法。
款3原例:1/3加1/6、1/3乘3/2都给出1/2;涉及INT_MAX的1/MAX加1/MAX、1/MAX乘MAX/2在一些实现上可能不良构,虽然数学约分结果可表示。原例演示的是本版中间表示的限制,不是让用户运行整数溢出。
2.4 20.10.5 [ratio.comparison] 款1–2 与其余比较声明
跳转到“2.4 20.10.5 [ratio.comparison] 款1–2 与其余比较声明”ratio_equal<R1,R2>(款1)在规范化num与den都相等时继承integral_constant<bool,true>,否则false。ratio_not_equal继承值为!ratio_equal<R1,R2>::value的integral_constant。
ratio_less<R1,R2>(款2)按数学交叉乘积R1::num*R2::den < R2::num*R1::den判断;允许用其他算法避免溢出,但若溢出发生则程序不良构。其余三者按以下关系定义:less_equal是!ratio_less<R2,R1>::value;greater是ratio_less<R2,R1>::value;greater_equal是!ratio_less<R1,R2>::value。这里没有运行期比较异常;不良构属于编译期程序有效性问题。
2.5 20.10.6 [ratio.si] 款1 与SI别名清单
跳转到“2.5 20.10.6 [ratio.si] 款1 与SI别名清单”款1:yocto、zepto、zetta、yotta只在其两个指定常量都能由intmax_t表示时定义;若任一个不能表示,则不得定义。不能假定所有实现都有这四个极端前缀。
以下按所给PDF603概览的可见数字列出,幂记号是该副本数字长度的阅读简写,不静默替换异常位数:
| 别名 | 该副本可见ratio常量 |
|---|---|
yocto | 1 / 10的24次方 |
zepto | 1 / 10的20次方;副本疑点见下 |
atto | 1 / 10的18次方 |
femto | 1 / 10的14次方;副本疑点见下 |
pico | 1 / 10的12次方 |
nano | 1 / 10的9次方 |
micro | 1 / 10的6次方 |
milli | 1 / 10的3次方 |
centi | 1 / 10的2次方 |
deci | 1 / 10 |
deca | 10 / 1 |
hecto | 10的2次方 / 1 |
kilo | 10的3次方 / 1 |
mega | 10的6次方 / 1 |
giga | 10的9次方 / 1 |
tera | 10的12次方 / 1 |
peta | 10的15次方 / 1 |
exa | 10的18次方 / 1 |
zetta | 10的21次方 / 1 |
yotta | 10的31次方 / 1;副本疑点见下 |
zepto、femto、yotta三行的可见长数字与通常SI前缀尺度不符,已实际查看该页图;本笔记不声称这是ISO最初出版物的同样错误,也不把外部SI表替换为此副本原文。用这些极端/有疑问别名作可移植设计前须取得可核实的清晰原文。以下原创例使用副本清晰、无需大整数边界的显式ratio。
#include <ratio>typedef std::ratio<1, 3> third;typedef std::ratio<1, 6> sixth;typedef std::ratio_add<third, sixth> half;static_assert(half::num == 1 && half::den == 2, "exact rational sum");静态图:模板实参N/D → 绝对值与非零分母检查 → gcd归一化 → num/den及规范化type;二元运算另经过“中间可表示或实现能避免中间溢出”的检查,才抵达最终ratio。
3 2011 20.11 chrono总览与时钟要求
跳转到“3 2011 20.11 chrono总览与时钟要求”3.1 20.11.1 [time.general] 款1 与20.11.2 [time.syn]
跳转到“3.1 20.11.1 [time.general] 款1 与20.11.2 [time.syn]”20.11包含chrono库与20.11.8的C时间函数。<chrono>在std::chrono中声明duration<Rep,Period=ratio<1>>、time_point<Clock,Duration=typename Clock::duration>、时间trait、运算与转换及三种钟;两个common_type特化在std中,而非chrono命名空间。概览还给6种常用时间跨度别名:
| 别名 | Period | 表示类型下限 |
|---|---|---|
nanoseconds | nano | 至少64位有符号整数 |
microseconds | micro | 至少55位有符号整数 |
milliseconds | milli | 至少45位有符号整数 |
seconds | ratio<1> | 至少35位有符号整数 |
minutes | ratio<60> | 至少29位有符号整数 |
hours | ratio<3600> | 至少23位有符号整数 |
不保证这些别名的rep恰为long long,也不把Period视为可在对象里任意修改的运行期成员。概览中的所有非成员算术/比较/转换在下文逐项列明;time_point相应操作在本版概览多数没有constexpr,不能统一补上现代声明。
3.2 20.11.3 [time.clock.req] 款1–4 表59
跳转到“3.2 20.11.3 [time.clock.req] 款1–4 表59”款1:clock把duration、time_point和取得当前点的now()打包;时间点原点称epoch。款2令t1、t2是两个有happens-before关系的now结果,并且两个调用都发生在该clock最大time_point之前,注说明排除期间回绕。表59逐行要求如下。
| 表达式/类型 | 要求类型 | 意义 |
|---|---|---|
C1::rep | 算术类型或模拟算术的类 | duration计数表示 |
C1::period | ratio特化 | 以秒为单位的tick周期 |
C1::duration | chrono::duration<C1::rep,C1::period> | 钟的跨度类型 |
C1::time_point | chrono::time_point<C1>,或chrono::time_point<C2,C1::duration> | 钟的时间点;C1与C2须指同一epoch |
C1::is_steady | const bool | 当t1总不大于t2且tick间隔恒定时真,否则假 |
C1::now() | C1::time_point | 表示当前时间点 |
款3注:钟报告跨度与SI时间定义的相对偏差是实现质量问题,分辨率与实际准确度不能混同。
款4 TrivialClock还须满足:本来就是Clock;rep、duration、time_point满足EqualityComparable、LessThanComparable、DefaultConstructible、CopyConstructible、CopyAssignable、Destructible及26.2数值类型要求(注特别指出这些类型操作不抛);这三种类型左值可交换;now不抛;TC::time_point::clock也递归满足TrivialClock。不是看到一个自定义类有静态now就可以略过其余要求。
4 2011 20.11.4 时间traits
跳转到“4 2011 20.11.4 时间traits”4.1 20.11.4.1 [time.traits.is_fp] 款1
跳转到“4.1 20.11.4.1 [time.traits.is_fp] 款1”template<class Rep>struct treat_as_floating_point : is_floating_point<Rep> {};duration据此判断不同tick周期的隐式转换策略。值为真则按浮点风格允许相应duration间隐式转换;否则还取决于周期比例。注说明该trait表达“像浮点那样能以可接受精度损失做除法”,并非只检测语法上能否写a/b。值为假时按整数式转换策略处理,最终仍须满足各构造接口要求。
4.2 20.11.4.2 [time.traits.duration_values] 款1–7
跳转到“4.2 20.11.4.2 [time.traits.duration_values] 款1–7”模板duration_values<Rep>提供静态constexpr函数zero、min、max,全部返回Rep。款1:用户自定义表示类型可能需要特殊实现,这时该类型作者应特化duration_values提供所需特殊值。
款2–3:zero返回Rep(0),必须是加法单位元;注解释不用Rep(),后者可能意味未初始化值。款4–5:min返回numeric_limits<Rep>::lowest(),其值不大于zero。款6–7:max返回numeric_limits<Rep>::max(),必须大于zero。对浮点rep,min不是最小正正规值,必须看lowest。
4.3 20.11.4.3 [time.traits.specializations] 款1–3
跳转到“4.3 20.11.4.3 [time.traits.specializations] 款1–3”duration二类型的common_type结果是duration,rep为两个Rep的common_type,period为两个Period的最大公约周期。款1注给计算法:分子取两Period分子的gcd,分母取两分母的lcm。款2注:这是让两个源duration转换时不用除法的最大tick周期;意图避免截断,但浮点仍可能舍入。
款3:同一个Clock的两个time_point共同类型仍用相同Clock,duration取两个Duration的common_type。这里没有为不同Clock建立自动共同时间轴。
本版general common_type原式见20C册20.9.7.6,没有现代decay修订。本段按本版特化引用它,不以现代实现的修补证明原文本对任意Rep都没有边界问题。
5 2011 20.11.5 duration
跳转到“5 2011 20.11.5 duration”5.1 20.11.5 [time.duration] 款1–6 与类概览
跳转到“5.1 20.11.5 [time.duration] 款1–6 与类概览”款1:duration保存tick数量与以秒计的ratio周期,用于度量两个time_point之间的时间;概览公开rep = Rep、period = Period,一个说明性成员rep_保存计数。
款2要求Rep是算术类型或模拟算术的类。款3:把另一个duration作为Rep,程序不良构。款4:Period不是ratio特化则不良构;款5:Period::num非正则不良构。款6:成员只可抛出它所调用的表示类型操作抛出的异常。
概览默认化默认构造、析构、复制构造和复制赋值,默认构造标constexpr。默认初始化并不普遍保证把算术rep清零;需要已知零值时使用duration::zero()或明确构造。原例的三个对象展示“long计分钟”“long long计毫秒”“double按1/30秒计tick”,不是许可读取未经合适初始化的计数。
5.2 20.11.5.1 [time.duration.cons] 款1–5
跳转到“5.2 20.11.5.1 [time.duration.cons] 款1–5”template<class Rep2>constexpr explicit duration(const Rep2& r);template<class Rep2,class Period2>constexpr duration(const duration<Rep2,Period2>& d);款1:计数构造只有Rep2隐式可转换到rep,且“目标rep按浮点处理”或“来源Rep2不按浮点处理”时参与重载。原例duration<int,milli>(3)成立,3.5不参与相应构造。explicit表示整数计数不能因此自动变成duration。款2构造对象,款3后置count() == static_cast<rep>(r)。
款4:从另一duration构造仅当目标按浮点处理,或周期商ratio_divide<Period2,period>::den == 1且源Rep2不按浮点处理时参与。注的意图是避免整数跨度的隐式截断。原例3毫秒可以隐式变成微秒,微秒不能反向隐式变成整数毫秒。款5以duration_cast<duration>(d).count()构造内部计数。
不要误把这项“tick比例不用除法”检查当作实际数值绝不会溢出的检查;即使类型转换可用,大计数仍受Rep算术范围约束。
5.3 20.11.5.2 [time.duration.observer] 款1
跳转到“5.3 20.11.5.2 [time.duration.observer] 款1”constexpr rep count() const返回rep_。它只返回tick数量,不能脱离Period就称为秒数。
5.4 20.11.5.3 [time.duration.arithmetic] 款1–20
跳转到“5.4 20.11.5.3 [time.duration.arithmetic] 款1–20”| 款 | 接口 | Effects/Returns |
|---|---|---|
| 1 | constexpr duration operator+() const | 返回*this的值 |
| 2 | constexpr duration operator-() const | 返回duration(-rep_) |
| 3–4 | duration& operator++() | ++rep_,返回*this |
| 5 | duration operator++(int) | 返回duration(rep_++),即旧计数 |
| 6–7 | duration& operator--() | --rep_,返回*this |
| 8 | duration operator--(int) | 返回duration(rep_--) |
| 9–10 | duration& operator+=(const duration& d) | rep_ += d.count();*this |
| 11–12 | duration& operator-=(const duration& d) | rep_ -= d.count();*this |
| 13–14 | duration& operator*=(const rep& rhs) | rep_ *= rhs;*this |
| 15–16 | duration& operator/=(const rep& rhs) | rep_ /= rhs;*this |
| 17–18 | duration& operator%=(const rep& rhs) | rep_ %= rhs;*this |
| 19–20 | duration& operator%=(const duration& rhs) | rep_ %= rhs.count();*this |
同类型复合赋值不自行选择新period。可声明这些成员也不意味着每个Rep都支持取模或所有值都能安全求负;除数为0、有符号最小值取负等语言边界不会被duration包装消除。
5.5 20.11.5.4 [time.duration.special] 款1–3
跳转到“5.5 20.11.5.4 [time.duration.special] 款1–3”三个静态constexpr duration函数:zero返回duration(duration_values<rep>::zero())(款1);min对应min(款2);max对应max(款3)。它们是可表示跨度的特殊值,不是某个钟的epoch。
5.6 20.11.5.5 [time.duration.nonmember] 款1–13
跳转到“5.6 20.11.5.5 [time.duration.nonmember] 款1–13”款1中CD表示该函数返回类型,CR(A,B)表示两表示类型的common_type。以下二元函数都以const引用接收实参并标constexpr,D1/D2表示各duration实例,P表示共享period。
| 款 | 操作与返回类型 | 规定返回/参与条件 |
|---|---|---|
| 2 | D1 + D2 → 两duration的common_type | CD(CD(lhs).count()+CD(rhs).count()) |
| 3 | D1 - D2 → 两duration的common_type | 相同转换后计数相减,再构造CD |
| 4–5 | duration<Rep1,P> * Rep2 → duration<CR(Rep1,Rep2),P> | Rep2隐式可转换到CR才参与;CD(CD(d).count()*s) |
| 6–7 | Rep1 * duration<Rep2,P> → 相应共同rep/P的duration | Rep1隐式可转换到CR才参与;返回d*s |
| 8–9 | duration<Rep1,P> / Rep2 → 共同rep/P的duration | Rep2隐式可转换到CR且不是duration实例才参与;CD(CD(d).count()/s) |
| 10 | D1 / D2 → common_type<Rep1,Rep2>::type | 副本Returns为CD(lhs).count()/CD(rhs).count(),与款1的CD约定存在疑点,见下 |
| 11–12 | duration<Rep1,P> % Rep2 → 共同rep/P的duration | Rep2隐式可转换到CR且不是duration实例才参与;转换后的count对s取模,再构造CD |
| 13 | D1 % D2 → 两duration的common_type | CD(CD(lhs).count()%CD(rhs).count()) |
第10款的数学角色是“跨度除以跨度得到无量纲计数”,但此副本的CD已按款1定义为返回表示类型,随后又对CD结果调用count,文字不自洽。需保留这项源文限制,不无提示用现代修正表达式替代。下面安全原创例避免依赖这一段有疑点的原式。
5.7 20.11.5.6 [time.duration.comparisons] 款1–7
跳转到“5.7 20.11.5.6 [time.duration.comparisons] 款1–7”款1令CT为两个duration实参类型的common_type;所有比较返回constexpr bool。款2相等为CT(lhs).count() == CT(rhs).count();款3不等为!(lhs == rhs);款4小于比较转换后的count;款5小于等于为!(rhs < lhs);款6大于为rhs < lhs;款7大于等于为!(lhs < rhs)。比较前的共同period转换是关键,不能只比较两个未经单位换算的裸count。
5.8 20.11.5.7 [time.duration.cast] 款1–2
跳转到“5.8 20.11.5.7 [time.duration.cast] 款1–2”template<class ToDuration,class Rep,class Period>constexpr ToDuration duration_cast(const duration<Rep,Period>& d);款1:ToDuration必须是duration实例,否则不参与重载。款2令CF = ratio_divide<Period,typename ToDuration::period>,CR = common_type<typename ToDuration::rep,Rep,intmax_t>::type。最后都以static_cast<ToDuration::rep>把结果转成目标表示并构造ToDuration;中间算式分四路:
| CF::num | CF::den | 最终转型之前的表达式 |
|---|---|---|
| 1 | 1 | 直接d.count(),无需中间乘除 |
| 非1 | 1 | static_cast<CR>(d.count()) * static_cast<CR>(CF::num) |
| 1 | 非1 | static_cast<CR>(d.count()) / static_cast<CR>(CF::den) |
| 非1 | 非1 | 先转CR、乘num,再除den |
注说明全用显式static_cast,编译期知道为1时省略相应运算,中间使用共同表示,末尾才转目标。它不是饱和算术,也不替用户检查所有溢出、浮点转整数范围或非有限值;整数除法精度损失仍可能存在。duration_cast明确表达转换,不自动保证数学结果完全精确。
原创安全例(2011):
#include <chrono>#include <ratio>
void duration_example_11() { typedef std::chrono::duration<int, std::ratio<1,1000>> int_milliseconds; int_milliseconds elapsed(7); elapsed += int_milliseconds(5); // 同类型同period,不依赖跨类型common_type int ticks = elapsed.count(); // 12个毫秒tick int_milliseconds zero = int_milliseconds::zero(); (void)ticks; (void)zero;}静态图:duration对象 = “Rep计数 × 编译期Period秒”;time_point = “某Clock epoch + duration”。两个count相同而Period不同,表达的跨度可能不同;两个duration相同而Clock不同,表达的绝对时间点也不能自动相互比较。
6 2011 20.11.6 time_point
跳转到“6 2011 20.11.6 time_point”6.1 20.11.6 [time.point] 款1–2 与概览
跳转到“6.1 20.11.6 [time.point] 款1–2 与概览”模板参数Clock与默认Clock::duration的Duration;公开别名clock、duration、rep、period,说明性成员d_储存距epoch的duration。款1要求Clock满足Clock requirements(副本交叉编号写20.11.7,具体要求在20.11.3);款2 Duration不是duration实例则不良构。
6.2 20.11.6.1 [time.point.cons] 款1–4
跳转到“6.2 20.11.6.1 [time.point.cons] 款1–4”time_point()以duration::zero()初始化d_,得到epoch(款1),不会去读取当前钟。explicit time_point(const duration& d)以d初始化,得到epoch+d(款2;explicit出现在类概览,详细接口重复时未重写)。
template<class Duration2> time_point(const time_point<clock,Duration2>& t)只有Duration2可隐式转换到duration时参与(款3),以t.time_since_epoch()初始化d_(款4)。两端clock相同;没有把任意钟时间点互相转换的本版构造。
6.3 20.11.6.2 [time.point.observer] 款1 与20.11.6.3 [time.point.arithmetic] 款1–4
跳转到“6.3 20.11.6.2 [time.point.observer] 款1 与20.11.6.3 [time.point.arithmetic] 款1–4”duration time_since_epoch() const返回d_。operator+=(const duration& d)对d_执行+=并返回*this(款1–2);operator-=对d_执行-=并返回*this(款3–4)。epoch由Clock定义,本版不统一规定为1970-01-01。
6.4 20.11.6.4 [time.point.special] 款1–2
跳转到“6.4 20.11.6.4 [time.point.special] 款1–2”static constexpr time_point min()返回time_point(duration::min()),max相应返回duration::max构造的点。这是表示范围端点,既不调用now,也不是“当前可用的最早/最晚系统日期”的额外外部保证。
6.5 20.11.6.5 [time.point.nonmember] 款1–4
跳转到“6.5 20.11.6.5 [time.point.nonmember] 款1–4”时间点加跨度返回相同Clock、采用两Duration共同类型的time_point,效果CT(lhs) += rhs(款1)。跨度加时间点返回反向顺序的相同加法(款2)。时间点减跨度返回lhs + (-rhs)(款3)。同Clock两个时间点相减,返回两个Duration共同类型的跨度,结果lhs.time_since_epoch() - rhs.time_since_epoch()(款4)。没有时间点加时间点接口。
6.6 20.11.6.6 [time.point.comparisons] 款1–6
跳转到“6.6 20.11.6.6 [time.point.comparisons] 款1–6”同Clock、可能不同Duration的两点比较返回bool:款1比较time_since_epoch相等;款2取相等否定;款3比较time_since_epoch小于;款4返回!(rhs < lhs);款5返回rhs < lhs;款6返回!(lhs < rhs)。不能因两个钟的rep/period碰巧相同就跨Clock比较。
6.7 20.11.6.7 [time.point.cast] 款1–2
跳转到“6.7 20.11.6.7 [time.point.cast] 款1–2”template<class ToDuration,class Clock,class Duration> time_point<Clock,ToDuration> time_point_cast(const time_point<Clock,Duration>& t)只有ToDuration为duration实例时参与(款1);返回time_point<Clock,ToDuration>(duration_cast<ToDuration>(t.time_since_epoch()))(款2)。它转换精度/表示,保留Clock,不是换钟或换时区。
7 2011 20.11.7 clocks与20.11.8 C时间
跳转到“7 2011 20.11.7 clocks与20.11.8 C时间”7.1 20.11.7 [time.clock] 款1
跳转到“7.1 20.11.7 [time.clock] 款1”本小节定义的三种钟都满足TrivialClock要求,不仅是Clock。
7.2 20.11.7.1 [time.clock.system] 款1–4
跳转到“7.2 20.11.7.1 [time.clock.system] 款1–4”款1 system_clock表示系统范围实时钟的wall-clock时间。公开rep、period、duration、time_point;period为未指定ratio;time_point为chrono::time_point<system_clock>;static const bool is_steady值未指定;static time_point now() noexcept读取当前点。
款2要求duration::min小于zero,注说明rep因此是有符号类型。款3 static time_t to_time_t(const time_point&) noexcept与款4 static time_point from_time_t(time_t) noexcept在二者精度中较粗的精度上代表同一时刻;究竟舍入还是截断由实现定义。不保证往返后保留所有细粒度tick,也不保证time_t本身是某个固定整数类型。
7.3 20.11.7.2 [time.clock.steady] 款1
跳转到“7.3 20.11.7.2 [time.clock.steady] 款1”steady_clock随物理时间前进其time_point不下降,并按稳定速率推进;不可被调节。rep、period未指定,duration按其二者组合,time_point在概览为chrono::time_point<unspecified,duration>;is_steady为true,now noexcept。适于持续时间测量的理由来自单调稳定性,不是声明其最小tick一定比system_clock更短。
7.4 20.11.7.3 [time.clock.hires] 款1
跳转到“7.4 20.11.7.3 [time.clock.hires] 款1”high_resolution_clock代表最短tick周期的钟,可以是system_clock或steady_clock别名。rep、period和is_steady未指定,now noexcept,time_point同样可以用未指定clock模板参数。不能由名字推断它必定单调、独立于其他钟,或具有最高实测准确度。
7.5 20.11.8 [date.time] 款1–2 表60 脚注231
跳转到“7.5 20.11.8 [date.time] 款1–2 表60 脚注231”款1表60列<ctime>:宏NULL、CLOCKS_PER_SEC;类型size_t、clock_t、time_t;结构tm;九个函数asctime、clock、difftime、localtime、strftime、ctime、gmtime、mktime、time。与2003副本表35相比,2011表60明确列了CLOCKS_PER_SEC,不能反向改写2003表的可见内容。
款2内容同C标准<time.h>,但明确asctime、ctime、gmtime、localtime不要求避免数据竞争(17.6.5.9)。不能在无同步并发上下文中依赖这些接口的可重入性,本笔记不做线程实验。原交叉引用写ISO C 7.12与Amendment 1 4.6.4,按副本保留,不暗换条号。
脚注231:strftime支持C转换说明符C、D、e、F、g、G、h、r、R、t、T、u、V、z。末尾修饰符在这份副本高倍率页图可见为“E and 0”;通常C时间格式的替代数字修饰符写为字母O,这是术语解释,不能冒称副本原字已印清楚或静默改字。此脚注没有把strftime输出解释为chrono格式库;后者在2011尚不属于本节。
8 2011 20.12 scoped_allocator_adaptor
跳转到“8 2011 20.12 scoped_allocator_adaptor”8.1 20.12.1 [allocator.adaptor.syn] 款1 与概览
跳转到“8.1 20.12.1 [allocator.adaptor.syn] 款1 与概览”<scoped_allocator>提供scoped_allocator_adaptor<OuterAlloc,InnerAllocs...>及两个不同outer但相同inner列表适配器之间的相等/不等比较。适配器公开继承OuterAlloc;OuterTraits是allocator_traits<OuterAlloc>的说明性别名。
款1:outer为容器自己的存储服务,inner传给每个元素的构造。只有一个allocator时,inner就是适配器自身,因而嵌套容器所有层递归复用相同资源;多于一个时依次向更深层传递,嵌套深度超过列表长度则最后一allocator重复使用。继承outer使适配器在多数表达式可作为outer使用;它不是自动为任意类增加接收allocator的构造函数。
静态图:容器第0层存储用A0 → 构造元素时传适配器(A1,A2) → 下一层用A1存储、向元素传A2 → 更深层持续复用最后A2。只有A0时,链的每一层都沿同一资源递归。
以下是按详细条款组织的接口索引;个别副本概览差异随后单列。
template<class OuterAlloc,class... InnerAllocs>class scoped_allocator_adaptor : public OuterAlloc {public: typedef OuterAlloc outer_allocator_type; typedef /* 按20.12.2 */ inner_allocator_type; typedef typename allocator_traits<OuterAlloc>::value_type value_type; typedef typename allocator_traits<OuterAlloc>::size_type size_type; typedef typename allocator_traits<OuterAlloc>::difference_type difference_type; typedef typename allocator_traits<OuterAlloc>::pointer pointer; typedef typename allocator_traits<OuterAlloc>::const_pointer const_pointer; typedef typename allocator_traits<OuterAlloc>::void_pointer void_pointer; typedef typename allocator_traits<OuterAlloc>::const_void_pointer const_void_pointer; // 三个propagate_on_container_*别名按20.12.2定义。 template<class Tp> struct rebind { typedef scoped_allocator_adaptor< typename allocator_traits<OuterAlloc>::template rebind_alloc<Tp>, InnerAllocs...> other; }; scoped_allocator_adaptor(); template<class OuterA2> scoped_allocator_adaptor(OuterA2&& outerAlloc, const InnerAllocs&... innerAllocs) noexcept; scoped_allocator_adaptor(const scoped_allocator_adaptor&) noexcept; scoped_allocator_adaptor(scoped_allocator_adaptor&&) noexcept; template<class OuterA2> scoped_allocator_adaptor( const scoped_allocator_adaptor<OuterA2,InnerAllocs...>&) noexcept; template<class OuterA2> scoped_allocator_adaptor( const scoped_allocator_adaptor<OuterA2,InnerAllocs...>&&) noexcept; ~scoped_allocator_adaptor(); inner_allocator_type& inner_allocator() noexcept; const inner_allocator_type& inner_allocator() const noexcept; outer_allocator_type& outer_allocator() noexcept; const outer_allocator_type& outer_allocator() const noexcept; pointer allocate(size_type n); pointer allocate(size_type n,const_void_pointer hint); void deallocate(pointer p,size_type n) noexcept; size_type max_size() const; template<class T,class... Args> void construct(T* p,Args&&... args); template<class T1,class T2,class... Args1,class... Args2> void construct(pair<T1,T2>* p,piecewise_construct_t, tuple<Args1...> x,tuple<Args2...> y); template<class T1,class T2> void construct(pair<T1,T2>* p); template<class T1,class T2,class U,class V> void construct(pair<T1,T2>* p,U&& x,V&& y); template<class T1,class T2,class U,class V> void construct(pair<T1,T2>* p,const pair<U,V>& x); template<class T1,class T2,class U,class V> void construct(pair<T1,T2>* p,pair<U,V>&& x); template<class T> void destroy(T* p); scoped_allocator_adaptor select_on_container_copy_construction() const;};该接口索引中的注释占位明确不是可编译定义,rebind把outer变为Tp对应分配器而保留inner列表。此处添加语法所需的依赖类型排版,仅为接口阅读;不声称这一整段就是副本原文逐字转录。621概览的通用construct缺少展开省略号,623详细声明有;621 deallocate没列noexcept,623详细接口有。跨outer的右值构造在621/622可见const ...&&,上方保留const,不套用后续版本改成非const形式。
8.2 20.12.2 [allocator.adaptor.types] 款1–4
跳转到“8.2 20.12.2 [allocator.adaptor.types] 款1–4”款1 inner_allocator_type:inner参数包为空时是scoped_allocator_adaptor<OuterAlloc>,非空时是scoped_allocator_adaptor<InnerAllocs...>。这里不是仅返回InnerAllocs第一个原始allocator类型,而是仍带递归能力的适配器。
款2复制赋值传播、款3移动赋值传播、款4交换传播这三个别名各自独立计算:只要OuterAlloc与所有InnerAllocs中任一个allocator_traits对应propagate_on_container_*::value为真,本适配器对应类型就为true_type;全部假才为false_type。是逐层逻辑或,不能误作逻辑与或只检查outer。
8.3 20.12.3 [allocator.adaptor.cnstr] 款1–9
跳转到“8.3 20.12.3 [allocator.adaptor.cnstr] 款1–9”款1默认构造对OuterAlloc基类与inner对象值初始化。款2–3的outer加inner参数构造要求OuterAlloc可由OuterA2构造;以std::forward<OuterA2>(outerAlloc)初始化基类,以其余实参递归初始化inner链。
款4同类型复制构造逐层从other对应allocator初始化;款5同类型移动构造逐层移动构造。款6–7跨outer的const左值形式要求OuterAlloc可由OuterA2构造,逐层用other对应allocator初始化。款8–9的跨outer右值形式同样有此要求,逐层使用对应allocator右值;副本声明实际带const,不能把其效果解释成必然调用能修改源的非const移动构造。除默认构造外这些声明明确noexcept;不凭用户自定义分配器任意抛异常来扩充标准保证。
8.4 20.12.4 [allocator.adaptor.members] 款1–8
跳转到“8.4 20.12.4 [allocator.adaptor.members] 款1–8”款1以说明型OUTERMOST递归取得最外层实际allocator:若x没有outer_allocator成员就用x,否则沿其outer_allocator递归。OUTERMOST_ALLOC_TRAITS以该对象类型形成allocator_traits。原注要求outer_allocator定义保证递归终止,所有scoped_allocator_adaptor实例的设计都应终止。这些大写名字不是公开宏/API。
源副本623首段递归表达式中可见x/z混用、括号残缺,trait表达式为allocator_traits<decltype(OUTERMOST(x))>,并未在字面去除引用。本段上面的递归解释依据同段自然语言及注,不是把残缺公式偷偷修成现代实现。outer返回条目也写Outer而类型别名是OuterAlloc;本册按其效果解释为返回outer基类,同时保留此字面差别。
| 款 | 接口 | Returns / Effects |
|---|---|---|
| 2 | const与非const inner_allocator() | inner包为空返回*this,否则返回说明性inner对象 |
| 3 | 非const outer_allocator() | 返回outer基类引用;副本Returns用static_cast<Outer&>(*this) |
| 4 | const outer_allocator() | 返回const outer基类引用;副本Returns用const Outer& |
| 5 | allocate(n) | allocator_traits<OuterAlloc>::allocate(outer_allocator(),n) |
| 6 | allocate(n,hint) | 同上加入hint |
| 7 | deallocate(p,n) noexcept | 通过outer的allocator_traits执行deallocate |
| 8 | max_size() const | 通过outer的allocator_traits查询max_size |
存储分配本身由outer完成,inner并不代替outer来申请容器自己的那块存储。
8.5 20.12.4 款9 普通元素构造
跳转到“8.5 20.12.4 款9 普通元素构造”construct(T* p,Args&&... args)依下列顺序选路径,每条检查都按原文所列Args类型而非自行增加引用修饰:
- 若
uses_allocator<T,inner_allocator_type>为假,且is_constructible<T,Args...>为真,用OUTERMOST_ALLOC_TRAITS向p转发原参数构造。 - 否则若uses_allocator为真,且T可由
allocator_arg_t,inner_allocator_type,Args...构造,则在原参数前加入allocator_arg,inner_allocator()。 - 否则若uses_allocator为真,且T可由
Args...,inner_allocator_type构造,则在原参数后加入inner_allocator()。 - 其他情况程序不良构。
注说明:trait声称需要allocator却没有相应构造,应报错而不是静默忘记把inner传入元素。每一路实际构造仍调用最外层allocator_traits,只有元素构造参数接收inner;两种“外/内”角色不可对调。
8.6 20.12.4 款10–11 pair分段构造
跳转到“8.6 20.12.4 款10–11 pair分段构造”接口接收pair<T1,T2>* p,piecewise_construct_t,tuple<Args1...> x,tuple<Args2...> y。款10要求两个参数包中的所有类型都CopyConstructible;不要依照后续更宽松修订删除此前提。
款11分别为T1/x和T2/y形成xprime/yprime,判断顺序与普通构造的三种uses-allocator路径对应:不使用allocator则原tuple不变;采用前缀约定则把包含allocator_arg与inner allocator引用的tuple接到参数tuple前;采用后缀约定则把inner allocator引用tuple接到后面;均不匹配则不良构。最后以OUTERMOST_ALLOC_TRAITS调用分段pair构造,传xprime、yprime。
关键副本字面:624给前缀tuple中保存inner_allocator_type&,却以inner_allocator_type()构造实参;后缀形式也如此,xprime与yprime共有四处。已核页图,不能悄悄换成inner_allocator()后再声称是原式。上段描述其allocator传播意图;本副本所印临时对象与非const左值引用的搭配问题独立列为疑点,不提供依赖该疑点路径的可运行示例。
8.7 20.12.4 款12–17 其他pair构造、销毁与复制选择
跳转到“8.7 20.12.4 款12–17 其他pair构造、销毁与复制选择”款12无实参pair构造转成this->construct(p,piecewise_construct,tuple<>(),tuple<>())。款13两个独立实参x/y分别以转发的forward_as_tuple打包,再走分段构造。款14从const pair复制,用forward_as_tuple(x.first)与forward_as_tuple(x.second);款15从pair右值,用std::forward<U>(x.first)与std::forward<V>(x.second)后打包。这些函数只在立即构造调用链内使用转发tuple,不把其结果存起来超出被引用值的生命周期。
款16 destroy(T* p)通过OUTERMOST_ALLOC_TRAITS的destroy销毁p处对象;不是对整个嵌套容器内存直接逐字节释放。款17 select_on_container_copy_construction() const返回新的适配器,其中每一层A都由对应原allocator调用allocator_traits<A>::select_on_container_copy_construction的结果初始化。是对每一层分别选择,而不是仅复制outer或无条件直接拷贝整个适配器。
8.8 20.12.5 [scoped.adaptor.operators] 款1–2
跳转到“8.8 20.12.5 [scoped.adaptor.operators] 款1–2”两个运算模板可比较OuterA1、OuterA2不同但InnerAllocs列表相同的适配器,形参为const引用,返回bool且noexcept。款1:无inner时仅比较outer;有inner时要求outer相等且inner适配器相等,递归覆盖所有层。款2不等是相等的否定。相等意义最终来自各allocator的相等/存储可互相回收契约,不是“对象地址相同”。
原创静态类型例:
#include <memory>#include <scoped_allocator>#include <type_traits>
typedef std::scoped_allocator_adaptor<std::allocator<int>> int_scope;static_assert(std::is_same<int_scope::inner_allocator_type, int_scope>::value, "one allocator repeats through inner levels");此例只检声明中的类型关系,不分配内存,不进入副本有疑问的pair分段构造公式。
9 2011 20.13 type_index
跳转到“9 2011 20.13 type_index”9.1 20.13.1 [type.index.synopsis] 与20.13.2 [type.index.overview] 款1
跳转到“9.1 20.13.1 [type.index.synopsis] 与20.13.2 [type.index.overview] 款1”<typeindex>声明type_index、hash主模板及hash<type_index>特化。款1说明type_index是type_info的简单包装,可在有序关联和无序关联容器中作为索引类型。它不是给所有C++源类型产生跨进程稳定字符串ID的工具。
class type_index {public: type_index(const type_info& rhs) noexcept; bool operator==(const type_index& rhs) const noexcept; bool operator!=(const type_index& rhs) const noexcept; bool operator<(const type_index& rhs) const noexcept; bool operator<=(const type_index& rhs) const noexcept; bool operator>(const type_index& rhs) const noexcept; bool operator>=(const type_index& rhs) const noexcept; size_t hash_code() const; const char* name() const;private: const type_info* target; // 仅说明};概览注说明保存指针而非引用使隐式复制/移动构造、赋值能按预期存在并工作。没有声明默认构造;hash_code()与name()在此2011副本的接口未显式写noexcept,不能为追随现代签名补写。
9.2 20.13.3 [type.index.members] 款1–9
跳转到“9.2 20.13.3 [type.index.members] 款1–9”| 款 | 成员 | Returns / Effects |
|---|---|---|
| 1 | 从const type_info& rhs构造 | 效果相当于target = &rhs |
| 2 | operator== | *target == *rhs.target |
| 3 | operator!= | *target != *rhs.target |
| 4 | operator< | target->before(*rhs.target) |
| 5 | operator<= | !rhs.target->before(*target) |
| 6 | operator> | rhs.target->before(*target) |
| 7 | operator>= | !target->before(*rhs.target) |
| 8 | hash_code() | target->hash_code() |
| 9 | name() | target->name() |
排序依type_info::before,不能写成type_info地址的内建指针小于,也不是比较name字符串。name、hash等的底层保证须回到本版18.7.1;本节没有加强成固定拼写、特定哈希算法或跨运行稳定值。
9.3 20.13.4 [type.index.hash] 款1
跳转到“9.3 20.13.4 [type.index.hash] 款1”template<> struct hash<type_index>满足20.8.12 hash要求。对index,hash<type_index>()(index)的结果必须与index.hash_code()相同。相同哈希不证明相同类型,冲突仍需相等比较区分。
原创安全例:
#include <typeindex>#include <typeinfo>
void type_index_example_11() { std::type_index a(typeid(int)); std::type_index b(typeid(int)); std::type_index c(typeid(double)); bool same = (a == b); // true bool different = (a != c); // true (void)same; (void)different;}此例只使用非求值的基础类型typeid,不假设name文本、不输出实现相关hash,也不依赖type_info对象的地址相等来判断类型。
10 覆盖台账与副本限制
跳转到“10 覆盖台账与副本限制”| 版本与范围 | 本册对应 |
|---|---|
| 2003 20.5款1–2表35 | 1.1 |
| 2011 20.10.1款1–2、20.10.2概览、20.10.3款1–2 | 2.1–2.2 |
| 2011 20.10.4款1–3表58、20.10.5款1–2及所有比较、20.10.6款1 | 2.3–2.5 |
| 2011 20.11.1款1、20.11.2概览、20.11.3款1–4表59 | 3.1–3.2 |
| 2011 20.11.4.1款1、20.11.4.2款1–7、20.11.4.3款1–3 | 4.1–4.3 |
| 2011 20.11.5款1–6、20.11.5.1款1–5、20.11.5.2款1 | 5.1–5.3 |
| 2011 20.11.5.3款1–20、20.11.5.4款1–3、20.11.5.5款1–13 | 5.4–5.6 |
| 2011 20.11.5.6款1–7、20.11.5.7款1–2 | 5.7–5.8 |
| 2011 20.11.6款1–2及20.11.6.1–7所有款 | 6.1–6.7 |
| 2011 20.11.7与三种钟全部款、20.11.8款1–2表60脚注231 | 7.1–7.5 |
| 2011 20.12.1款1及全部概览、20.12.2款1–4、20.12.3款1–9 | 8.1–8.3 |
| 2011 20.12.4款1–17、20.12.5款1–2 | 8.4–8.8 |
| 2011 20.13.1概览、20.13.2款1、20.13.3款1–9、20.13.4款1 | 9.1–9.3 |
本册源范围没有缺失整页;所有上述小节、编号款和接口族均已对应。已经核页图的副本可见限制包括:603三种SI别名数字异常;614 duration除duration的CD说明与返回式冲突;619脚注末修饰符可见字形0;621/623 construct包展开与deallocate异常规格差别;621/622跨outer转换构造保留const右值;623 OUTERMOST递归残缺及Outer名字;624给inner allocator引用tuple传inner_allocator_type()的四处字面。以上只报告所给副本,不认定ISO原始出版物同样有误。没有用现代DR把这些公式替换后伪称完成原文复原。
原文引入的接口条件、复杂度若未规定则不自行补充;本册也不把noexcept等同“调用目标不会失败”、不把ratio精确算术等同所有中间计算无溢出、不把时间分辨率等同准确度、不把分配器传播等同所有容器层共用同一状态。