I just noticed the following code in <chrono.h>, which doesn’t make sense to me.
struct system_clock
{
static const bool is_monotonic = false; // retained
static const bool is_steady = false;
};
class steady_clock
: public system_clock
{ // wraps monotonic clock
public:
static const bool is_monotonic = true; // retained
static const bool is_steady = true;
};
typedef steady_clock monotonic_clock; // retained
typedef system_clock high_resolution_clock;
How can steady_clock be steady when it simply derives from system_clock which is not steady?
Ignoring bugs in Microsoft’s implementation for the moment, having a steady clock derive from an unsteady one (or a monotonic one derive from a non-monotonic one) makes perfectly good sense in general.
This is one of those places that the typical “is-a” terminology gets in the way, and you need to really think in terms of substitution instead. In particular, the situation is not “a steady clock is not an unsteady clock, so the derivation is wrong.” Rather, the situation is “a steady clock can be substituted for an unsteady clock under any circumstances, so the derivation is fine” (and likewise for
is_monotonic).Let’s consider an extreme example — having an atomic clock connected directly to your computer. It’s monotonic and about as steady as you can hope to get. Assuming its output is high enough frequency (/resolution), you could use it in place of essentially any/every other clock your system might have available.