我注意到对于我测试过的每个系统,std::chrono::high_treatment_clock::period::num = 1
。是否存在任何系统(嵌入式、桌面、移动或其他)它恰好是其他数字? (在这样的系统上,1 秒不能用刻度来表示。)
最佳答案
据我所知,std::chrono::high_resolution_clock
有三种实现:Visual Studio、gcc 和 clang(与 libc++ 一起使用时)。
所有这三个都具有纳秒精度(std::chrono::high_resolution_clock::period::num = 1
)。对于 VS 和 libc++,high_resolution_clock
的类型别名为 steady_clock
。在 gcc 上,它的类型别名为 system_clock
。
规范中没有任何内容可以阻止 std::chrono::high_treatment_clock::period::num != 1
,并且您是正确的,在这样的系统上 1 秒无法表示在“蜱”中。这进一步转化为:
seconds
would not be implicitly convertible tohigh_resolution_clock::duration
.
要找到秒
和high_resolution_clock::duration
可转换的最粗略持续时间,您可以方便地使用:
using CT = common_type_t<seconds, high_resolution_clock::duration>;
对于我所知道的所有实现,CT
是纳秒
的类型别名。
关于c++ - 特定于平台的 std::chrono::high_resolution_clock::period::num,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47687645/