<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 4057: generator::iterator's operator* is not noexcept when it can be</title>
<meta property="og:title" content="Issue 4057: generator::iterator's operator* is not noexcept when it can be">
<meta property="og:description" content="C++ library issue. Status: New">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue4057.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="4057"><a href="lwg-active.html#4057">4057</a>. <code>generator::iterator</code>'s <code>operator*</code> is not <code>noexcept</code> when it can be</h3>
<p><b>Section:</b> 25.8.6 <a href="https://wg21.link/coro.generator.iterator">[coro.generator.iterator]</a> <b>Status:</b> <a href="lwg-active.html#New">New</a>
 <b>Submitter:</b> S. B. Tam <b>Opened:</b> 2024-03-01 <b>Last modified:</b> 2024-09-17</p>
<p><b>Priority: </b>3
</p>
<p><b>View other</b> <a href="lwg-index-open.html#coro.generator.iterator">active issues</a> in [coro.generator.iterator].</p>
<p><b>View all other</b> <a href="lwg-index.html#coro.generator.iterator">issues</a> in [coro.generator.iterator].</p>
<p><b>View all issues with</b> <a href="lwg-status.html#New">New</a> status.</p>
<p><b>Discussion:</b></p>
<p>
<code>generator::<i>iterator</i></code>'s <code>operator*</code> is specified to have the following noexcept-specifier:
</p>
<blockquote><pre>
noexcept(is_nothrow_copy_constructible_v&lt;<i>reference</i>&gt;)
</pre></blockquote>
<p>
When <code><i>reference</i></code> is an rvalue reference type, <code>is_nothrow_copy_constructible_v&lt;<i>reference</i>&gt;</code>
is <code>false</code> (because <code><i>reference</i></code> is not copy constructible), which means <code>operator*</code>
is not <code>noexcept</code>.
<p/>
However, <code>operator*</code> doesn't perform any potentially-throwing operation in this case. It's effect is
equivalent to <code>return static_cast&lt;<i>reference</i>&gt;(*p.<i>value_</i>);</code> (where the type of <code>p.<i>value_</i></code>
is effectively <code>add_pointer_t&lt;<i>reference</i>&gt;</code>). Neither the dereference nor the cast to rvalue
reference can throw an exception.
<p/>
I think the expression inside the noexcept-specifier should be changed to
<code>noexcept(static_cast&lt;<i>reference</i>&gt;(*p.<i>value_</i>))</code>.
</p>

<p><i>[2024-06-24; Reflector poll]</i></p>

<p>
Set priority to 3 after reflector poll.
"Just remove the noexcept-specifier."
"Maybe add <code>is_reference_v&lt;reference&gt; || ...</code>"
</p>

<p><i>[2024-09-16; Casey provides wording]</i></p>

<p>
In the prioritization thread, LWG discussed a couple of alternatives without much enthusiasm. One
of the alternatives was to simply remove the <i>noexcept-specifier</i>, which seems to be the most
efficient use of LWG's time.
</p>



<p id="res-4057"><b>Proposed resolution:</b></p>
<p>
This wording is relative to <a href="https://wg21.link/N4988" title=" Working Draft, Programming Languages — C++">N4988</a>.
</p>

<ol>
<li><p>Modify 25.8.6 <a href="https://wg21.link/coro.generator.iterator">[coro.generator.iterator]</a> as indicated:</p>
<blockquote>
<pre>
namespace std {
  template&lt;class Ref, class Val, class Allocator&gt;
  class generator&lt;Ref, Val, Allocator&gt;::<i>iterator</i> {
    [&hellip;]
    <i>reference</i> operator*() const <del>noexcept(is_nothrow_copy_constructible_v&lt;reference&gt;)</del>;
    [&hellip;]
  };
}
</pre>
[&hellip;]
<p>-3- <i>Returns</i>: <code>*this</code>.</p>
<pre>
<i>reference</i> operator*() const <del>noexcept(is_nothrow_copy_constructible_v&lt;<i>reference</i>&gt;l)</del>;
</pre>
<p>-4- <i>Preconditions</i>: For some generator object <code>x</code>, <i><code>coroutine_</code></i> is in <code>*x.<i>active_</i></code> and
<code>x.<i>active_</i>-&gt;top()</code> refers to a suspended coroutine with promise object <code>p</code>.</p>
</blockquote>
</li>
</ol>






</body>
</html>
