【问题标题】:platform-specific std::chrono::high_resolution_clock::period::num平台特定的 std::chrono::high_resolution_clock::period::num
【发布时间】:2018-05-21 02:56:37
【问题描述】:

我注意到std::chrono::high_resolution_clock::period::num = 1 用于我测试过的每个系统。是否存在任何恰好是其他数字的系统(嵌入式、桌面、移动或其他)? (在这样的系统上,1 秒无法以刻度表示。)

【问题讨论】:

    标签: c++ time std


    【解决方案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_resolution_clock::period::num != 1,您说得对,在这样的系统上,1 秒不能用“滴答声”表示。这进一步转化为:

    seconds 不会隐式转换为 high_resolution_clock::duration

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

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

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

    【讨论】:

      猜你喜欢
      • 2017-09-02
      • 2014-03-23
      • 2015-05-14
      • 2017-08-31
      • 1970-01-01
      • 2015-06-04
      • 2016-09-22
      • 1970-01-01
      • 2017-11-25
      相关资源
      最近更新 更多