Title
Disallow chrono::duration<const T, P>
Status
new
Section
[time.duration.general]
Submitter
Jonathan Wakely

Created on 2025-11-26.00:00:00 last changed 2 weeks ago

Messages

Date: 2025-11-26.09:37:33

Proposed resolution:

This wording is relative to N5014.

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

    -2- `Rep` shall be an arithmetic type or a class emulating an arithmetic type. If a specialization of `duration` is instantiated with a cv-qualified type or a specialization of `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.

Date: 2025-11-26.00:00:00

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>

History
Date User Action Args
2025-11-26 09:37:33adminsetmessages: + msg15760
2025-11-26 00:00:00admincreate