This page is a snapshot from the LWG issues list, see the Library Active Issues List for more information and the meaning of New status.

4481. Disallow chrono::duration<const T, P>

Section: 30.5.1 [time.duration.general] Status: New Submitter: Jonathan Wakely Opened: 2025-11-26 Last modified: 2025-11-26

Priority: Not Prioritized

View all issues with New status.

Discussion:

Using a const type as the rep for a chrono::duration causes various problems but there seems to be no rule preventing it.

The non-const member operators that modify a duration don't work if the rep type is const, e.g. duration<const int>::operator++() is typically ill-formed (unless the implementation chooses to store remove_cv_t<rep> as the data member). hash<duration<const int>> uses hash<const int> which is not enabled, so you can't hash a duration with a const rep. Generic code that wants to perform arithmetic with the rep type would need to remember to consistently use remove_cv_t<rep> to work correctly with const types.

We should just disallow const rep types. If you want a non-modifiable duration, use const duration<R,P> not duration<const R, P>

Proposed resolution:

This wording is relative to N5014.

  1. Modify 30.5.1 [time.duration.general] as indicated:

    -2- Rep shall be an arithmetic type or a class emulating an arithmetic type. If duration is instantiated with a duration type as the argument for the template parameter Rep, the program is ill-formed.

    -3- If Period is not a specialization of ratio, the program is ill-formed. If Period::num is not positive, the program is ill-formed.

    -4- Members of duration do not throw exceptions other than those thrown by the indicated operations on their representations.