平台特定 std::chrono::high_resolution_clock::period::num

platform-specific std::chrono::high_resolution_clock::period::num

我注意到 std::chrono::high_resolution_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_clocksteady_clock 的类型别名。在 gcc 上它是类型别名 system_clock.

规范中没有任何内容可以阻止 std::chrono::high_resolution_clock::period::num != 1,并且您是正确的,在这样的系统上 1 秒不能在 "ticks" 中表示。这进一步转化为:

seconds would not be implicitly convertible to high_resolution_clock::duration.

要找到 secondshigh_resolution_clock::duration 都可转换为的最粗略持续时间,您可以方便地使用:

using CT = common_type_t<seconds, high_resolution_clock::duration>;

对于我所知道的所有实现,CTnanoseconds 的类型别名。