<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 4308: std::optional&lt;T&amp;&gt;::iterator can't be a contiguous iterator for some T</title>
<meta property="og:title" content="Issue 4308: std::optional&lt;T&amp;&gt;::iterator can't be a contiguous iterator for some T">
<meta property="og:description" content="C++ library issue. Status: New">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue4308.html">
<meta property="og:type" content="website">
<meta property="og:image" content="http://cplusplus.github.io/LWG/images/cpp_logo.png">
<meta property="og:image:alt" content="C++ logo">
<style>
  p {text-align:justify}
  li {text-align:justify}
  pre code.backtick::before { content: "`" }
  pre code.backtick::after { content: "`" }
  blockquote.note
  {
    background-color:#E0E0E0;
    padding-left: 15px;
    padding-right: 15px;
    padding-top: 1px;
    padding-bottom: 1px;
  }
  ins {background-color:#A0FFA0}
  del {background-color:#FFA0A0}
  table.issues-index { border: 1px solid; border-collapse: collapse; }
  table.issues-index th { text-align: center; padding: 4px; border: 1px solid; }
  table.issues-index td { padding: 4px; border: 1px solid; }
  table.issues-index td:nth-child(1) { text-align: right; }
  table.issues-index td:nth-child(2) { text-align: left; }
  table.issues-index td:nth-child(3) { text-align: left; }
  table.issues-index td:nth-child(4) { text-align: left; }
  table.issues-index td:nth-child(5) { text-align: center; }
  table.issues-index td:nth-child(6) { text-align: center; }
  table.issues-index td:nth-child(7) { text-align: left; }
  table.issues-index td:nth-child(5) span.no-pr { color: red; }
  @media (prefers-color-scheme: dark) {
     html {
        color: #ddd;
        background-color: black;
     }
     ins {
        background-color: #225522
     }
     del {
        background-color: #662222
     }
     a {
        color: #6af
     }
     a:visited {
        color: #6af
     }
     blockquote.note
     {
        background-color: rgba(255, 255, 255, .10)
     }
  }
</style>
</head>
<body>
<hr>
<p><em>This page is a snapshot from the LWG issues list, see the <a href="lwg-active.html">Library Active Issues List</a> for more information and the meaning of <a href="lwg-active.html#New">New</a> status.</em></p>
<h3 id="4308"><a href="lwg-active.html#4308">4308</a>. <code>std::optional&lt;T&amp;&gt;::iterator</code> can't be a contiguous iterator for some <code class='backtick'>T</code></h3>
<p><b>Section:</b> 22.5.4.5 <a href="https://wg21.link/optional.ref.iterators">[optional.ref.iterators]</a> <b>Status:</b> <a href="lwg-active.html#New">New</a>
 <b>Submitter:</b> Jiang An <b>Opened:</b> 2025-08-05 <b>Last modified:</b> 2025-08-29</p>
<p><b>Priority: </b>1
</p>
<p><b>View all issues with</b> <a href="lwg-status.html#New">New</a> status.</p>
<p><b>Discussion:</b></p>
<p>
This is related to LWG <a href="lwg-active.html#4304" title="std::optional&lt;NonReturnable&amp;&gt; is ill-formed due to value_or (Status: New)">4304</a><sup><a href="https://cplusplus.github.io/LWG/issue4304" title="Latest snapshot">(i)</a></sup>. When <code class='backtick'>T</code> is function type or an incomplete array type, 
it is impossible to implement all requirements in 22.5.4.5 <a href="https://wg21.link/optional.ref.iterators">[optional.ref.iterators]</a>/1.
<p/>
When <code class='backtick'>T</code> is an incomplete object type, we may want to support <code>std::optional&lt;T&amp;&gt;</code> 
as it's sometimes a replacement of <code class='backtick'>T*</code>. Perhaps we can require that the iterator type is always a 
random access iterator, and additional models <code class='backtick'>contiguous_iterator</code> when <code class='backtick'>T</code> is complete.
<p/>
When <code class='backtick'>T</code> is a function type, the possibly intended iterator would be not even an actual iterator. 
But it seems that range-for loop over such an <code>std::optional&lt;T&amp;&gt;</code> can work.
</p>

<p><i>[2025-08-29; Reflector poll]</i></p>

<p>
Set priority to 1 after reflector poll.
</p>
<p>
"How can <code class='backtick'>end()</code> work for a pointer to incomplete type? <code class='backtick'>begin</code>/<code class='backtick'>end</code> should
be constrained on object types, and Mandate complete object types.
The aliases shouldn't be defined for non-object types, but probably harmless."
</p>



<p id="res-4308"><b>Proposed resolution:</b></p>
<p>
This wording is relative to <a href="https://wg21.link/N5014">N5014</a>.
</p>

<ol>

<li><p>Modify 22.5.4.5 <a href="https://wg21.link/optional.ref.iterators">[optional.ref.iterators]</a> as indicated:</p>

<blockquote>
<pre>
using iterator = <i>implementation-defined</i>; 
</pre>
<blockquote>
<p>
-1- <del>T</del><ins>If <code class='backtick'>T</code> is an object type, t</ins>his type models <del><code class='backtick'>contiguous_iterator</code> 
(24.3.4.14 <a href="https://wg21.link/iterator.concept.contiguous">[iterator.concept.contiguous]</a>)</del><ins><code class='backtick'>random_access_iterator</code> 
(24.3.4.13 <a href="https://wg21.link/iterator.concept.random.access">[iterator.concept.random.access]</a>)</ins>, meets the 
<i>Cpp17RandomAccessIterator</i> requirements (24.3.5.7 <a href="https://wg21.link/random.access.iterators">[random.access.iterators]</a>), and meets 
the requirements for constexpr iterators (24.3.1 <a href="https://wg21.link/iterator.requirements.general">[iterator.requirements.general]</a>), with value 
type <code>remove_cv_t&lt;T&gt;</code>. The reference type is <code>T&amp;</code> for <code class='backtick'>iterator</code>.
<ins>When <code class='backtick'>T</code> is a complete object type, iterator additionally models <code class='backtick'>contiguous_iterator</code> 
(24.3.4.14 <a href="https://wg21.link/iterator.concept.contiguous">[iterator.concept.contiguous]</a>).</ins><br/> 
<del>-2-</del> All requirements on container iterators (23.2.2.2 <a href="https://wg21.link/container.reqmts">[container.reqmts]</a>) apply to 
<code class='backtick'>optional::iterator</code>.
<p/>
<ins>-?-  If <code class='backtick'>T</code> is a function type, <code class='backtick'>iterator</code> supports all operators required by the 
<code class='backtick'>random_access_iterator</code> concept (24.3.4.13 <a href="https://wg21.link/iterator.concept.random.access">[iterator.concept.random.access]</a>) along with the 
<code>&lt;=&gt;</code> operator as specified for container iterators (23.2.2.2 <a href="https://wg21.link/container.reqmts">[container.reqmts]</a>). 
<code class='backtick'>iterator</code> dereferences to a <code class='backtick'>T</code> lvalue. These operators behave as if <code class='backtick'>iterator</code> were an actual 
iterator iterating over a range of <code class='backtick'>T</code>, and result in constant subexpressions whenever the behavior 
is well-defined. [<i>Note ?</i>: Such an <code class='backtick'>optional::iterator</code> does not need to declare any member 
type because it is not an actual iterator type. &mdash; <i>end note</i>]</ins>
</p>
</blockquote>
</blockquote>

</li>

</ol>





</body>
</html>
