|
| 1 | +<?xml version='1.0' encoding='utf-8' standalone='no'?> |
| 2 | +<!DOCTYPE issue SYSTEM "lwg-issue.dtd"> |
| 3 | + |
| 4 | +<issue num="4481" status="New"> |
| 5 | +<title>Disallow <tt>chrono::duration<const T, P></tt></title> |
| 6 | +<section><sref ref="[time.duration.general]"/></section> |
| 7 | +<submitter>Jonathan Wakely</submitter> |
| 8 | +<date>26 Nov 2025</date> |
| 9 | +<priority>99</priority> |
| 10 | + |
| 11 | +<discussion> |
| 12 | +<p> |
| 13 | +Using a const type as the `rep` for a `chrono::duration` causes various problems |
| 14 | +but there seems to be no rule preventing it. |
| 15 | +</p> |
| 16 | +<p> |
| 17 | +The non-const member operators that modify a `duration` don't work if |
| 18 | +the `rep` type is const, e.g. |
| 19 | +<code>duration<const int>::operator++()</code> |
| 20 | +is typically ill-formed (unless the implementation chooses to store |
| 21 | +<code>remove_cv_t<rep></code> as the data member). |
| 22 | +<code>hash<duration<const int>></code> uses |
| 23 | +<code>hash<const int></code> which is not enabled, |
| 24 | +so you can't hash a `duration` with a const rep. |
| 25 | +Generic code that wants to perform arithmetic with the `rep` type |
| 26 | +would need to remember to consistently use <code>remove_cv_t<rep></code> |
| 27 | +to work correctly with const types. |
| 28 | +</p> |
| 29 | +<p> |
| 30 | +We should just disallow const `rep` types. |
| 31 | +If you want a non-modifiable duration, |
| 32 | +use <code>const duration<R,P></code> |
| 33 | +not <code>duration<const R, P></code> |
| 34 | +</p> |
| 35 | + |
| 36 | +</discussion> |
| 37 | + |
| 38 | +<resolution> |
| 39 | +<p> |
| 40 | +This wording is relative to <paper num="N5014"/>. |
| 41 | +</p> |
| 42 | + |
| 43 | +<ol> |
| 44 | +<li><p>Modify <sref ref="[time.duration.general]"/> as indicated:</p> |
| 45 | + |
| 46 | +<blockquote> |
| 47 | +<p>-2- |
| 48 | +`Rep` shall be an arithmetic type or a class emulating an arithmetic type. |
| 49 | +If <ins>a specialization of</ins> `duration` is instantiated with a |
| 50 | +<ins>cv-qualified type or a specialization of</ins> `duration` <del>type</del> |
| 51 | +as the argument for the template parameter `Rep`, the program is ill-formed. |
| 52 | +</p> |
| 53 | +<p>-3- |
| 54 | +If `Period` is not a specialization of `ratio`, the program is ill-formed. |
| 55 | +If `Period::num` is not positive, the program is ill-formed. |
| 56 | +</p> |
| 57 | +<p>-4- |
| 58 | +Members of `duration` do not throw exceptions other than those thrown by |
| 59 | +the indicated operations on their representations. |
| 60 | +</p> |
| 61 | +</blockquote> |
| 62 | +</li> |
| 63 | +</ol> |
| 64 | + |
| 65 | +</resolution> |
| 66 | + |
| 67 | +</issue> |
0 commit comments