Kevin Yin
Kevin Yin

Reputation: 882

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

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.)

Upvotes: 2

Views: 349

Answers (1)

Howard Hinnant
Howard Hinnant

Reputation: 219428

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.

Upvotes: 4

Related Questions