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

删除回忆录丶 提交于 2020-01-04 05:53:51

问题


I've noticed that std::chrono::high_resolution_clock::period::num = 1 for every system I've tested. Does there exist any system (embedded, desktop, mobile, or otherwise) where it happens to be some other number? (On such a system, 1 second would not be representable in ticks.)


回答1:


There are three implementations of std::chrono::high_resolution_clock that I am aware of: Visual Studio, gcc and clang (when used with libc++).

All three of these have nanosecond-precision (std::chrono::high_resolution_clock::period::num = 1). For VS and libc++, high_resolution_clock is type-aliased to steady_clock. On gcc it is type-aliased to system_clock.

There is nothing in the spec that prevents std::chrono::high_resolution_clock::period::num != 1, and you are correct that on such a system 1 second would not be representable in "ticks". This further translates to:

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

To find the coarsest duration to which both seconds and high_resolution_clock::duration are convertible to, you can portably use:

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

For all of the implementations I'm aware of, CT is a type-alias for nanoseconds.



来源:https://stackoverflow.com/questions/47687645/platform-specific-stdchronohigh-resolution-clockperiodnum

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!