In the standards paper P0092R1, Howard Hinnant wrote:
template <class To, class Rep, class Period,
class = enable_if_t<detail::is_duration<To>{}>>
constexpr
To floor(const duration<Rep, Period>& d)
{
To t = duration_cast<To>(d);
if (t > d)
--t;
return t;
}
How can this code work? The problem is that operator--
on a std::chrono::duration
is not a constexpr operation. It is defined as:
duration& operator--();
And yet this code compiles, and gives the right answer at compile time:
static_assert(floor<hours>(minutes{3}).count() == 0, "”);
What's up with that?
See Question&Answers more detail:os