Search code examples
c++literalsc++20c++-chrono

Why std::chrono::day stored value is unspecified if d > 255


Quoted from std::literals::chrono_literals::operator""d

A std::chrono::day storing d. If d > 255, the stored value is unspecified.

What is the rationale behind this limit ?


Solution

  • The literal can't store more than std::chrono::day itself can, which is 0-255. This allows it to be implemented as a 1 byte data type, which as it usually will only store values between 1 and 31 is sufficient.

    If you're looking for a data type to store a duration of an arbitrary number of days use std::chrono::days instead.