<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 3609: std::ranges::iota_view&lt;int, long&gt; has non-subtractable iterator and 
sentinel types</title>
<meta property="og:title" content="Issue 3609: std::ranges::iota_view&lt;int, long&gt; has non-subtractable iterator and 
sentinel types">
<meta property="og:description" content="C++ library issue. Status: New">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue3609.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="3609"><a href="lwg-active.html#3609">3609</a>. <code>std::ranges::iota_view&lt;int, long&gt;</code> has non-subtractable <code><i>iterator</i></code> and 
<code><i>sentinel</i></code> types</h3>
<p><b>Section:</b> 25.6.4.4 <a href="https://wg21.link/range.iota.sentinel">[range.iota.sentinel]</a> <b>Status:</b> <a href="lwg-active.html#New">New</a>
 <b>Submitter:</b> Jiang An <b>Opened:</b> 2021-09-25 <b>Last modified:</b> 2021-10-14</p>
<p><b>Priority: </b>3
</p>
<p><b>View all issues with</b> <a href="lwg-status.html#New">New</a> status.</p>
<p><b>Discussion:</b></p>
<p>
Currently <code>std::ranges::iota_view&lt;int, long&gt;</code> uses its special <code><i>sentinel</i></code> type, as its 
<code>W</code> and <code>Bound</code> are different types and <code>Bound</code> is not <code>std::unreachable_sentinel_t</code>. 
However, as <code>W</code> (<code>int</code>) is not an iterator type, the iterator and sentinel types don't satisfy 
<code>std::sized_sentinel_for</code>, and thus not subtractable.
<p/>
IMO we should handle <code>operator-</code> overloads for <code>iota_view::<i>iterator</i></code> and <code>iota_view::<i>sentinel</i></code> 
like <code>iota_view::size</code> and <code>operator-</code> for <code>iota_view::<i>iterator</i></code>.
</p>

<p><i>[2021-10-14; Reflector poll]</i></p>

<p>
Set priority to 3 after reflector poll.
</p>
<p><i>[Tim Song commented:]</i></p>

<p>
We might be able to simplify the <code>(y_value > x.value_)</code> conditional
since we know that we are working with an iterator and its sentinel.
</p>



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

<ol>
<li><p>Modify 25.6.4.4 <a href="https://wg21.link/range.iota.sentinel">[range.iota.sentinel]</a> as indicated:</p>

<blockquote>
<blockquote>
<pre>
namespace std::ranges {
  template&lt;weakly_incrementable W, semiregular Bound&gt;
    requires <i>weakly-equality-comparable-with</i>&lt;W, Bound&gt; &amp;&amp; copyable&lt;W&gt;
  struct iota_view&lt;W, Bound&gt;::<i>sentinel</i> {
  private:
    Bound <i>bound_</i> = Bound(); <i>// exposition only</i>
  public:
    <i>sentinel</i>() = default;
    constexpr explicit <i>sentinel</i>(Bound bound);
    
    friend constexpr bool operator==(const <i>iterator</i>&amp; x, const <i>sentinel</i>&amp; y);
    
    friend constexpr <del>iter_difference_t&lt;W&gt;</del><ins><i>IOTA-DIFF-T</i>(W)</ins> operator-(const <i>iterator</i>&amp; x, const <i>sentinel</i>&amp; y)
      requires <ins>(<i>is-integer-like</i>&lt;W&gt; &amp;&amp; <i>is-integer-like</i>&lt;Bound&gt;) ||</ins> sized_sentinel_for&lt;Bound, W&gt;;

    friend constexpr <del>iter_difference_t&lt;W&gt;</del><ins><i>IOTA-DIFF-T</i>(W)</ins> operator-(const <i>sentinel</i>&amp; x, const <i>iterator</i>&amp; y)
      requires <ins>(<i>is-integer-like</i>&lt;W&gt; &amp;&amp; <i>is-integer-like</i>&lt;Bound&gt;) ||</ins> sized_sentinel_for&lt;Bound, W&gt;;
  };
}
</pre>
</blockquote>
<p>
[&hellip;]
</p>
<pre>
friend constexpr <del>iter_difference_t&lt;W&gt;</del><ins><i>IOTA-DIFF-T</i>(W)</ins> operator-(const <i>iterator</i>&amp; x, const <i>sentinel</i>&amp; y)
  requires <ins>(<i>is-integer-like</i>&lt;W&gt; &amp;&amp; <i>is-integer-like</i>&lt;Bound&gt;) ||</ins> sized_sentinel_for&lt;Bound, W&gt;;
</pre>
<blockquote>
<p>
-3- <i>Effects:</i> Equivalent to: <del><code>return x.<i>value_</i> - y.<i>bound_</i>;</code></del>
</p>
<blockquote><pre>
<ins>using D = <i>IOTA-DIFF-T</i>(W);
if constexpr (<i>is-integer-like</i>&lt;W&gt;) {
  auto y_value = W(y.<i>bound_</i>);
  if constexpr (<i>is-signed-integer-like</i>&lt;W&gt;) {
    return D(D(x.<i>value_</i>) - D(y_value));
  } else {
    return (y_value > x.<i>value_</i>)
      ? D(-D(y_value - x.<i>value_</i>))
      : D(x.<i>value_</i> - y_value);
  }
} else {
  return x.<i>value_</i> - y.<i>bound_</i>;
}</ins>
</pre></blockquote>
</blockquote>
<pre>
friend constexpr <del>iter_difference_t&lt;W&gt;</del><ins><i>IOTA-DIFF-T</i>(W)</ins> operator-(const <i>sentinel</i>&amp; x, const <i>iterator</i>&amp; y)
  requires <ins>(<i>is-integer-like</i>&lt;W&gt; &amp;&amp; <i>is-integer-like</i>&lt;Bound&gt;) ||</ins> sized_sentinel_for&lt;Bound, W&gt;;
</pre>
<blockquote>
<p>
-4- <i>Effects:</i> Equivalent to: <code>return -(y - x);</code>
</p>
</blockquote>
</blockquote>
</li>

</ol>





</body>
</html>
