问题
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:
secondswould not be implicitly convertible tohigh_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