Skip to content

Commit cec1f0e

Browse files
committed
New issue from Jiang An: "std::optional<T&>::iterator can't be a contiguous iterator for some T"
1 parent 6d07890 commit cec1f0e

File tree

1 file changed

+70
-0
lines changed

1 file changed

+70
-0
lines changed

xml/issue4308.xml

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?xml version='1.0' encoding='utf-8' standalone='no'?>
2+
<!DOCTYPE issue SYSTEM "lwg-issue.dtd">
3+
4+
<issue num="4308" status="New">
5+
<title><tt>std::optional&lt;T&amp;&gt;::iterator</tt> can't be a contiguous iterator for some `T`</title>
6+
<section>
7+
<sref ref="[optional.ref.iterators]"/>
8+
</section>
9+
<submitter>Jiang An</submitter>
10+
<date>05 Aug 2025</date>
11+
<priority>99</priority>
12+
13+
<discussion>
14+
<p>
15+
This is related to LWG <iref ref="4304"/>. When `T` is function type or an incomplete array type,
16+
it is impossible to implement all requirements in <sref ref="[optional.ref.iterators]"/>/1.
17+
<p/>
18+
When `T` is an incomplete object type, we may want to support <tt>std::optional&lt;T&amp;&gt;</tt>
19+
as it's sometimes a replacement of `T*`. Perhaps we can require that the iterator type is always a
20+
random access iterator, and additional models `contiguous_iterator` when `T` is complete.
21+
<p/>
22+
When `T` is a function type, the possibly intended iterator would be not even an actual iterator.
23+
But it seems that range-for loop over such an <tt>std::optional&lt;T&amp;&gt;</tt> can work.
24+
</p>
25+
</discussion>
26+
27+
<resolution>
28+
<p>
29+
This wording is relative to this
30+
<a href="https://github.com/cplusplus/draft/actions/runs/16749320058/artifacts/3690555293">CD preview draft</a>.
31+
</p>
32+
33+
<ol>
34+
35+
<li><p>Modify <sref ref="[optional.ref.iterators]"/> as indicated:</p>
36+
37+
<blockquote>
38+
<pre>
39+
using iterator = <i>implementation-defined</i>;
40+
</pre>
41+
<blockquote>
42+
<p>
43+
-1- <del>T</del><ins>If `T` is an object type, t</ins>his type models <del>`contiguous_iterator`
44+
(<sref ref="[iterator.concept.contiguous]"/>)</del><ins>`random_access_iterator`
45+
(<sref ref="[iterator.concept.random.access]"/>)</ins>, meets the
46+
<i>Cpp17RandomAccessIterator</i> requirements (<sref ref="[random.access.iterators]"/>), and meets
47+
the requirements for constexpr iterators (<sref ref="[iterator.requirements.general]"/>), with value
48+
type <tt>remove_cv_t&lt;T&gt;</tt>. The reference type is <tt>T&amp;</tt> for `iterator`.
49+
<ins>When `T` is a complete object type, iterator additionally models `contiguous_iterator`
50+
(<sref ref="[iterator.concept.contiguous]"/>).</ins><br/>
51+
<del>-2-</del> All requirements on container iterators (<sref ref="[container.reqmts]"/>) apply to
52+
`optional::iterator`.
53+
<p/>
54+
<ins>-?- If `T` is a function type, `iterator` supports all operators required by the
55+
`random_access_iterator` concept (<sref ref="[iterator.concept.random.access]"/>) along with the
56+
<tt>&lt;=&gt;</tt> operator as specified for container iterators (<sref ref="[container.reqmts]"/>).
57+
`iterator` dereferences to a `T` lvalue. These operators behave as if `iterator` were an actual
58+
iterator iterating over a range of `T`, and result in constant subexpressions whenever the behavior
59+
is well-defined. [<i>Note ?</i>: Such an `optional::iterator` does not need to declare any member
60+
type because it is not an actual iterator type. &mdash; <i>end note</i>]</ins>
61+
</p>
62+
</blockquote>
63+
</blockquote>
64+
65+
</li>
66+
67+
</ol>
68+
</resolution>
69+
70+
</issue>

0 commit comments

Comments
 (0)