Search code examples
c++language-lawyerc++17c++-chronoc++-concepts

The Clock and TrivialClock concepts in the chrono library


The std::chrono library relies, in part, on the concept of a Clock and a TrivialClock. I went through the standard, but I am still not sure about a few things:

  • Is the only difference between Clock and TrivialClock, the fact that now may throw for a Clock, but does not for a TrivialClock?
  • Is clock::duration required to be the same as clock::time_point::duration?
  • Is now required to be a static function member, or not?

References/quotes to the standard are welcome.


Solution

    • Is the only difference between Clock and TrivialClock, the fact that now may throw for a Clock, but does not for a TrivialClock?

    No. There are also more requirements on the member types of TrivialClock. A clock with a const int as its rep is not a TrivialClock, but it is a Clock.

    • Is clock::duration required to be the same as clock::time_point::duration?

    Yes. The two possible instantiations are std::time_point<clock>, which has clock::duration from the default parameter; or std::time_point<other_clock, clock::duration>, which has it explicitly.

    • Is now required to be a static function member, or not?

    Not as far as I can tell, but people are likely to assume that it is. All that is required is for clock::now() to be a valid expression, which returns a clock::time_point. It could be a static functor.