Skip to content

Commit 109f3cb

Browse files
committed
New issue from Jonathan: Disallow chrono::duration<const T, P>
1 parent b9a9190 commit 109f3cb

File tree

1 file changed

+67
-0
lines changed

1 file changed

+67
-0
lines changed

xml/issue4481.xml

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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&lt;const T, P&gt;</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&lt;const int&gt;::operator++()</code>
20+
is typically ill-formed (unless the implementation chooses to store
21+
<code>remove_cv_t&lt;rep&gt;</code> as the data member).
22+
<code>hash&lt;duration&lt;const int&gt;&gt;</code> uses
23+
<code>hash&lt;const int&gt;</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&lt;rep&gt;</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&lt;R,P&gt;</code>
33+
not <code>duration&lt;const R, P&gt;</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

Comments
 (0)