Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How is chrono::steady_clock's rep and period types determined?

chrono::steady_clock consists of 4 member types: rep, period, duration, and timepoint.

chrono::steady_clock::duration is a chrono::duration<rep,period>. chrono::steady_clock::period is a ratio.

How are the types of chrono::steady_clock::duration and chrono::steady_clock::period determined?

For example,

chrono::steady_clock::duration could be chrono::duration<int,period> where period is ratio<1,1000>

or

chrono::steady_clock::duration could be chrono::duration<double,period> where period is ratio<1,1000000>.

like image 933
user3731622 Avatar asked Oct 19 '15 21:10

user3731622


2 Answers

chrono::steady_clock consists of 4 member types: rep, period, duration, and timepoint.

chrono::stead_clock::duration is a chrono::duration<rep,period>. chrono::steady_clock::period is a ratio.

Yes.

How are the types of chrono::stead_clock::duration and chrono::steady_clock::period determined?

They are somewhat implementation specific, but there do exist constraints and invariants among these types.

1. rep must be a pretty well-behaved arithmetic-like type. It must have a default constructor, copy constructor, copy assignment and it has non-surprising semantics, and doesn't throw exceptions. The de facto standard is that all steady_clock::rep are signed integral 64 bit types.

2. period is pretty much up in the air. That being said, VS, libstdc++ and libc++ all have steady_clock::period set to nano.

3. duration must have a very specific relationship to rep and period: is_same<duration, chrono::duration<rep, period>>{} is true_type. It doesn't matter much if you specify duration first and derive rep and period from that, or vice-versa. But the invariant must hold.

4. time_point shall be an instantiation of std::chrono::time_point with a duration of duration. It is unspecified what the time_point's clock is. However what type that clock is, it must have the same epoch as steady_clock. This is arguably a defect and should have the constraint that steady_clock::time_point::clock is steady_clock. The de facto standard is that this constraint holds.

like image 145
Howard Hinnant Avatar answered Sep 21 '22 22:09

Howard Hinnant


The rep and period are unspecified by the standard. Each standard library implementation may choose their own. If the underlying timing calls they use have a precision of 100ns, choosing a period of std::ratio<100, 1000000000> seems like a good idea.

There are a few requirements those types must satisfy, but it's pretty much up to the implementation.

duration is always just based on rep and period... std::chrono::duration<rep, period>

like image 31
David Avatar answered Sep 19 '22 22:09

David