<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 3748: common_iterator and counted_iterator' operator- are missing cast to return type</title>
<meta property="og:title" content="Issue 3748: common_iterator and counted_iterator' operator- are missing cast to return type">
<meta property="og:description" content="C++ library issue. Status: New">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue3748.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="3748"><a href="lwg-active.html#3748">3748</a>. <code>common_iterator</code> and <code>counted_iterator</code>' <code>operator-</code> are missing cast to return type</h3>
<p><b>Section:</b> 24.5.5.6 <a href="https://wg21.link/common.iter.cmp">[common.iter.cmp]</a>, 24.5.7.5 <a href="https://wg21.link/counted.iter.nav">[counted.iter.nav]</a> <b>Status:</b> <a href="lwg-active.html#New">New</a>
 <b>Submitter:</b> Hewill Kang <b>Opened:</b> 2022-08-01 <b>Last modified:</b> 2022-08-23</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>
Both <code>common_iterator</code> and <code>counted_iterator</code> explicitly specify that the 
return type of their <code>operator-</code> is <code>iter_difference_t&lt;I2&gt;</code>, however, 
given that the calculated type may be <code>iter_difference_t&lt;I&gt;</code>, we should do 
an explicit conversion here since the latter is not necessarily implicitly convertible to the former:
</p>
<blockquote><pre>
#include &lt;ranges&gt;

struct Y;

struct X {
  X(Y);
  using difference_type =
#ifdef __GNUC__
  std::ranges::__detail::__max_diff_type;
#elif defined(_MSC_VER)
  std::_Signed128;
#endif
  int&amp; operator*() const;
  X&amp; operator++();
  void operator++(int);
};

struct Y {
  using difference_type = std::ptrdiff_t;
  int&amp; operator*() const;
  Y&amp; operator++();
  void operator++(int);
};

int main() {
  std::counted_iterator&lt;Y&gt; y;
  return std::counted_iterator&lt;X&gt;(y) - y; // <span style="color:red;font-weight:bolder">hard error in stdlibc++ and MSVC-STL</span>
}
</pre></blockquote>
<p>
<b>Daniel:</b>
</p>
<blockquote class="note">
<p>
This issue shouldn't we voted until a decision for LWG <a href="lwg-defects.html#3749" title="common_iterator should handle integer-class difference types (Status: WP)">3749</a><sup><a href="https://cplusplus.github.io/LWG/issue3749" title="Latest snapshot">(i)</a></sup> has been made, because the first part of
it overlaps with LWG <a href="lwg-defects.html#3749" title="common_iterator should handle integer-class difference types (Status: WP)">3749</a><sup><a href="https://cplusplus.github.io/LWG/issue3749" title="Latest snapshot">(i)</a></sup>'s second part.
</p>
</blockquote>

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

<p>
Set priority to 3 after reflector poll.
</p>
<p>
"I think <code>common_iterator</code> should <em>reject</em> iterators with
integer-class difference types since it can't possibly achieve the design intent
of adapting them to <i>Cpp17Iterator</i>s, so this issue should only affect
<code>counted_iterator</code>."
</p>
<p>
"If the difference types of <code>I</code> and <code>I2</code> are different
then the <code>operator-</code> can't be used to model
<code>sized_sentinel_for</code>,
since <code>i - i2</code> and <code>i2 - i</code> would have different types.
Providing <code>operator-</code> under such circumstances seems
to be of dubious value."
</p>



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

<ol>
<li><p>Modify 24.5.5.6 <a href="https://wg21.link/common.iter.cmp">[common.iter.cmp]</a> as indicated:</p>

<blockquote>
<pre>
template&lt;sized_sentinel_for&lt;I&gt; I2, sized_sentinel_for&lt;I&gt; S2>
  requires sized_sentinel_for&lt;S, I2&gt;
friend constexpr iter_difference_t&lt;I2&gt; operator-(
  const common_iterator&amp; x, const common_iterator&lt;I2, S2&gt;&amp; y);
</pre>
<blockquote>
<p>
-5- <i>Preconditions</i>: <code>x.v_.valueless_by_exception()</code> and <code>y.v_.valueless_by_exception()</code> 
are each <code>false</code>.
<p/>
-6- <i>Returns</i>: <code>0</code> if <code><i>i</i></code> and <code><i>j</i></code> are each <code>1</code>, and otherwise 
<ins><code>static_cast&lt;iter_difference_t&lt;I2&gt;&gt;(</code></ins><code>get&lt;<i>i</i>&gt;(x.v_) - 
get&lt;<i>j</i>&gt;(y.v_)</code><ins><code>)</code></ins>, where <code><i>i</i></code> is <code>x.v_.index()</code> 
and <code><i>j</i></code> is <code>y.v_.index()</code>.
</p>
</blockquote>
</blockquote>
</li>

<li><p>Modify 24.5.7.5 <a href="https://wg21.link/counted.iter.nav">[counted.iter.nav]</a> as indicated:</p>

<blockquote>
<pre>
template&lt;common_with&lt;I&gt; I2&gt;
  friend constexpr iter_difference_t&lt;I2&gt; operator-(
    const counted_iterator&amp; x, const counted_iterator&lt;I2&gt;&amp; y);
</pre>
<blockquote>
<p>
-13- <i>Preconditions</i>: <code>x</code> and <code>y</code> refer to elements of the same sequence (24.5.7.1 <a href="https://wg21.link/counted.iterator">[counted.iterator]</a>).
<p/>
-14- <i>Effects</i>: Equivalent to: <code>return <ins>static_cast&lt;iter_difference_t&lt;I2&gt;&gt;(</ins>y.length - 
  x.length<ins>)</ins>;</code>
</p>
</blockquote>
</blockquote>
</li>

</ol>





</body>
</html>
