<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 3852: join_with_view::iterator's iter_move and iter_swap should be conditionally noexcept</title>
<meta property="og:title" content="Issue 3852: join_with_view::iterator's iter_move and iter_swap should be conditionally noexcept">
<meta property="og:description" content="C++ library issue. Status: New">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue3852.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="3852"><a href="lwg-active.html#3852">3852</a>. <code>join_with_view::<i>iterator</i></code>'s <code>iter_move</code> and <code>iter_swap</code> should be conditionally <code>noexcept</code></h3>
<p><b>Section:</b> 25.7.15.3 <a href="https://wg21.link/range.join.with.iterator">[range.join.with.iterator]</a> <b>Status:</b> <a href="lwg-active.html#New">New</a>
 <b>Submitter:</b> Hewill Kang <b>Opened:</b> 2023-01-06 <b>Last modified:</b> 2024-01-29</p>
<p><b>Priority: </b>3
</p>
<p><b>View other</b> <a href="lwg-index-open.html#range.join.with.iterator">active issues</a> in [range.join.with.iterator].</p>
<p><b>View all other</b> <a href="lwg-index.html#range.join.with.iterator">issues</a> in [range.join.with.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>
In order to preserve room for optimization, the standard always tries to
propagate the <code>noexcept</code> specification of custom
<code>iter_move</code>/<code>iter_swap</code> for different iterators.
</p>
<p>But for <code>join_with_view::<i>iterator</i></code>,
these two specializations are the only ones in the standard that do not have
a <code>noexcept</code> specification.
This is because both invoke <code>visit</code> in the function body,
and <code>visit</code> may throw an exception when the <code>variant</code>
does not hold a value.
</p>
<p>
However, implementors are not required to follow the standard practice.
Since the <code>join_with_view::<i>iterator</i></code>'s <code>variant</code> member
only contains two alternative types, both libstdc++ and MSVC-STL avoid
heavyweight <code>visit</code> calls by simply using multiple if statements.
This means that it is still possible to add a conditional <code>noexcept</code>
specification to these overloads, and there is already a precedent in the
standard, namely <code>common_iterator</code>.
All we need to do is add a <i>Preconditions</i>.
</p>

<p><i>[2023-02-01; Reflector poll]</i></p>

<p>
Set priority to 3 after reflector poll.
"The iter_swap specification is wrong since we can swap Pattern and Inner.
And this is something implementations can strengthen."
</p>



<p id="res-3852"><b>Proposed resolution:</b></p>
<p>
This wording is relative to <a href="https://wg21.link/N4917" title=" Working Draft, Standard for Programming Language C++">N4917</a>.
</p>

<ol>
<li><p>Modify 25.7.15.3 <a href="https://wg21.link/range.join.with.iterator">[range.join.with.iterator]</a> as indicated:</p>

<blockquote>
<blockquote>
<pre>
namespace std::ranges {
  template&lt;input_range V, forward_range Pattern&gt;
  requires view&lt;V&gt; &amp;&amp; input_range&lt;range_reference_t&lt;V&gt;&gt;
        &amp;&amp; view&lt;Pattern&gt; &amp;&amp; <i>compatible-joinable-ranges</i>&lt;range_reference_t&lt;V&gt;, Pattern&gt;
  template&lt;bool Const&gt;
  class join_with_view&lt;V, Pattern&gt;::<i>iterator</i> {
    [&hellip;]
    <i>Parent</i>* <i>parent_</i> = nullptr;                                          <i>// exposition only</i>
    <i>OuterIter</i> <i>outer_it_</i> = <i>OuterIter()</i>;                                  <i>// exposition only</i>
    variant&lt;<i>PatternIter</i>, <i>InnerIter</i>&gt; <i>inner_it_</i>;                          <i>// exposition only</i>
    [&hellip;]
  public:
    [&hellip;]
    friend constexpr decltype(auto) iter_move(const <i>iterator</i>&amp; x) <ins>noexcept(<i>see below</i>);</ins> <del>{
      using rvalue_reference = common_reference_t&lt;
        iter_rvalue_reference_t&lt;<i>InnerIter</i>&gt;,
        iter_rvalue_reference_t&lt;<i>PatternIter</i>&gt;&gt;;
      return visit&lt;rvalue_reference&gt;(ranges::iter_move, x.<i>inner_it_</i>);
    }</del>

    friend constexpr void iter_swap(const <i>iterator</i>&amp; x, const <i>iterator</i>&amp; y) <ins>noexcept(<i>see below</i>)</ins>
      requires indirectly_swappable&lt;<i>InnerIter</i>, <i>PatternIter</i>&gt;<ins>;</ins><del>{
      visit(ranges::iter_swap, x.<i>inner_it_</i>, y.<i>inner_it_</i>);
    }</del>
  };
}
</pre>
</blockquote>
[&hellip;]
<pre><ins>friend constexpr decltype(auto) iter_move(const <i>iterator</i>&amp; x) noexcept(<i>see below</i>);</ins>
</pre>
<blockquote>
<p>
<ins>
-?- Let <code><i>rvalue_reference</i></code> be: 
</ins>
<pre>
<ins>  common_reference_t&lt;iter_rvalue_reference_t&lt;<i>InnerIter</i>&gt;, iter_rvalue_reference_t&lt;<i>PatternIter</i>&gt;&gt;
</ins></pre>
</p>
<p>
<ins>
-?- <i>Preconditions</i>: <code>x.<i>inner_it_</i>.valueless_by_exception()</code> is <code>false</code>.
</ins>
</p>
<p>
<ins>
-?- <i>Effects</i>: Equivalent to: <code>return visit&lt;<i>rvalue_reference</i>&gt;(ranges::iter_move, x.<i>inner_it_</i>);</code>
</ins>
</p>
<p>
<ins>
-?- <i>Remarks</i>: The exception specification is equivalent to:
</ins>
<pre>
<ins>  noexcept(ranges::iter_move(declval&lt;const <i>InnerIter</i>&amp;&gt;())) &amp;&amp;
  noexcept(ranges::iter_move(declval&lt;const <i>PatternIter</i>&amp;&gt;())) &amp;&amp;
  is_nothrow_convertible_v&lt;iter_rvalue_reference_t&lt;<i>InnerIter</i>&gt;, <i>rvalue_reference</i>&gt; &amp;&amp;
  is_nothrow_convertible_v&lt;iter_rvalue_reference_t&lt;<i>PatternIter</i>&gt;, <i>rvalue_reference</i>&gt;
</ins></pre>
</p>
</blockquote>
<pre><ins>friend constexpr void iter_swap(const <i>iterator</i>&amp; x, const <i>iterator</i>&amp; y) noexcept(<i>see below</i>)
  requires indirectly_swappable&lt;<i>InnerIter</i>, <i>PatternIter</i>&gt;;</ins>
</pre>
<blockquote>
<p>
<ins>
-?- <i>Preconditions</i>: <code>x.<i>inner_it_</i>.valueless_by_exception()</code> and <code>y.<i>inner_it_</i>.valueless_by_exception()</code> are each <code>false</code>.
</ins>
</p>
<p>
<ins>
-?- <i>Effects</i>: Equivalent to: <code>visit(ranges::iter_swap, x.<i>inner_it_</i>, y.<i>inner_it_</i>)</code>.
</ins>
</p>
<p>
<ins>
-?- <i>Remarks</i>: The exception specification is equivalent to:
</ins>
<pre>
<ins>  noexcept(ranges::iter_swap(declval&lt;const <i>InnerIter</i>&amp;&gt;(), declval&lt;const <i>InnerIter</i>&amp;&gt;())) &amp;&amp;
  noexcept(ranges::iter_swap(declval&lt;const <i>PatternIter</i>&amp;&gt;(), declval&lt;const <i>PatternIter</i>&amp;&gt;()))</ins></pre>
</p>
</blockquote>
</blockquote>
</li>
</ol>






</body>
</html>
