<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 3763: Should range adaptor iterators only provide iterator_category when its 
difference_type is not an integer-class type?</title>
<meta property="og:title" content="Issue 3763: Should range adaptor iterators only provide iterator_category when its 
difference_type is not an integer-class type?">
<meta property="og:description" content="C++ library issue. Status: New">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue3763.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="3763"><a href="lwg-active.html#3763">3763</a>. Should range adaptor iterators only provide <code>iterator_category</code> when its 
<code>difference_type</code> is not an integer-class type?</h3>
<p><b>Section:</b> 25.6.5.3 <a href="https://wg21.link/range.repeat.iterator">[range.repeat.iterator]</a>, 25.7.33.3 <a href="https://wg21.link/range.cartesian.iterator">[range.cartesian.iterator]</a> <b>Status:</b> <a href="lwg-active.html#New">New</a>
 <b>Submitter:</b> Hewill Kang <b>Opened:</b> 2022-08-27 <b>Last modified:</b> 2023-02-07</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>
After <a href="https://wg21.link/P2259" title=" Repairing input range adaptors and counted_iterator">P2259</a>, the range adaptor' iterators only provide <code>iterator_category</code> member when 
the underlying range models <code>forward_range</code>, which is mainly based on the premise that all valid C++20 
forward iterators meet the C++17 input iterator requirements.
<p/>
However, this is not strictly correct. When the underlying range's <code>difference_type</code> is an integer-class 
type, its iterator does not conform <i>Cpp17InputIterator</i>.
<p/>
Although <code>iterator_traits&lt;I&gt;::iterator_category</code> will still deduce the correct category in this 
case since these iterators have no <code>reference</code> member, it might be misleading to provide these 
incorrect member types. 
<p/>
Do we need to aggressively prohibit these iterators from providing <code>iterator_category</code> when their 
difference type is an integer-class type?
<p/>
The proposed resolution makes <code>repeat_view::<i>iterator</i></code> conditionally provide 
<code>iterator_category</code>, because it explicitly mentions <code><i>IOTA-DIFF-T</i>(<i>index-type</i>)</code> 
in the definition of <code>difference_type</code>, which makes it consistent with LWG <a href="lwg-defects.html#3670" title="Cpp17InputIterators don't have integer-class difference types (Status: C++23)">3670</a><sup><a href="https://cplusplus.github.io/LWG/issue3670" title="Latest snapshot">(i)</a></sup>.
<p/>
It also removes the <code>reference</code> member type of <code>cartesian_product_view::<i>iterator</i></code>, 
which prevents <code>iterator_traits&lt;I&gt;::iterator_category</code> from being aliased to its member 
<code>iterator_category</code>, so that <code>iterator_traits&lt;I&gt;::iterator_category</code> will not always 
be an <code>input_iterator_tag</code> when its <code>difference_type</code> is an integer-class type.
<p/>
This is also consistent with other range adaptors, as none of them have a <code>reference</code> member type.
</p>

<p><i>[2022-09-23; Reflector poll]</i></p>

<p>
Set priority to 3 after reflector poll.
</p>



<p id="res-3763"><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.6.5.3 <a href="https://wg21.link/range.repeat.iterator">[range.repeat.iterator]</a> as indicated:</p>
<blockquote>
<pre>
namespace std::ranges {
  template&lt;move_constructible W, semiregular Bound = unreachable_sentinel_t&gt;
    requires (is_object_v&lt;W> &amp;&amp; same_as&lt;W, remove_cv_t&lt;W&gt;&gt; &amp;&amp;
              (<i>is-integer-like</i>&lt;Bound> || same_as&lt;Bound, unreachable_sentinel_t&gt;))
  class repeat_view&lt;W, Bound>::<i>iterator</i> {
  private:
    using <i>index-type</i> =                  <i>// exposition only</i>
      conditional_t&lt;same_as&lt;Bound, unreachable_sentinel_t&gt;, ptrdiff_t, Bound&gt;;
    const W* <i>value_</i> = nullptr;          <i>// exposition only</i>
    <i>index-type</i> <i>current_</i> = <i>index-type</i>(); <i>// exposition only</i>

    constexpr explicit <i>iterator</i>(const W* value, <i>index-type</i> b = <i>index-type</i>());   <i>// exposition only</i>

  public:
    using iterator_concept = random_access_iterator_tag;
    using iterator_category = random_access_iterator_tag;           <ins><i>// present only if difference_type</i></ins>
                                                                    <ins><i>// is an integral type</i></ins>
    using value_type = W;
    using difference_type = conditional_t&lt;<i>is-signed-integer-like</i>&lt;<i>index-type</i>&gt;,
        <i>index-type</i>,
        <i>IOTA-DIFF-T</i>(<i>index-type</i>)>; 
    [&hellip;]
  };
}
</pre>
</blockquote>
</li>

<li><p>Modify 25.7.33.3 <a href="https://wg21.link/range.cartesian.iterator">[range.cartesian.iterator]</a> as indicated:</p>
<blockquote>
<blockquote>
<pre>
namespace std::ranges {
  template&lt;input_range First, forward_range... Vs&gt;
    requires (view&lt;First&gt; &amp;&amp; ... &amp;&amp; view&lt;Vs&gt;)
  template&lt;bool Const&gt;
  class cartesian_product_view&lt;First, Vs...&gt;::<i>iterator</i> {
  public:
    using iterator_category = input_iterator_tag;
    using iterator_concept  = <i>see below</i>;
    using value_type = tuple&lt;range_value_t&lt;<i>maybe-const</i>&lt;Const, First&gt;&gt;,
      range_value_t&lt;<i>maybe-const</i>&lt;Const, Vs&gt;&gt;...&gt;;
    <del>using reference = tuple&lt;range_reference_t&lt;<i>maybe-const</i>&lt;Const, First&gt;&gt;,
      range_reference_t&lt;<i>maybe-const</i>&lt;Const, Vs&gt;&gt;...&gt;;</del>
    using difference_type = <i>see below</i>;
    [&hellip;]
  
    constexpr <ins>auto</ins><del>reference</del> operator[](difference_type n) const
      requires <i>cartesian-product-is-random-access</i>&lt;Const, First, Vs...&gt;;
  
    [&hellip;]
  };
}
</pre>
</blockquote>
<p>
[&hellip;]
</p>
<pre>
constexpr <ins>auto</ins><del>reference</del> operator[](difference_type n) const
  requires <i>cartesian-product-is-random-access</i>&lt;Const, First, Vs...&gt;;
</pre>
<blockquote>
<p>
-24- <i>Effects</i>: <code>return *((*this) + n);</code>
</p>
</blockquote>
</blockquote>
</li>

</ol>





</body>
</html>
