<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 3281: Conversion from pair-like types to subrange is a silent semantic promotion</title>
<meta property="og:title" content="Issue 3281: Conversion from pair-like types to subrange is a silent semantic promotion">
<meta property="og:description" content="C++ library issue. Status: C++20">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue3281.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#C++20">C++20</a> status.</em></p>
<h3 id="3281"><a href="lwg-defects.html#3281">3281</a>. Conversion from <code><i>pair-like</i></code> types to <code>subrange</code> is a silent semantic promotion</h3>
<p><b>Section:</b> 25.5.4 <a href="https://wg21.link/range.subrange">[range.subrange]</a> <b>Status:</b> <a href="lwg-active.html#C++20">C++20</a>
 <b>Submitter:</b> Eric Niebler <b>Opened:</b> 2019-09-10 <b>Last modified:</b> 2021-02-25</p>
<p><b>Priority: </b>1
</p>
<p><b>View all other</b> <a href="lwg-index.html#range.subrange">issues</a> in [range.subrange].</p>
<p><b>View all issues with</b> <a href="lwg-status.html#C++20">C++20</a> status.</p>
<p><b>Discussion:</b></p>
<p>
Just because a <code>pair</code> is holding two iterators, it doesn't mean those two iterators denote a 
valid range. Implicitly converting such <code><i>pair-like</i></code> types to a <code>subrange</code> is 
dangerous and should be disallowed.
<p/>
Suggested priority P1.
<p/>
Implementation experience: range-v3's subrange lacks these constructors.
</p>

<p><i>[2019-10 Priority set to 1 and status to LEWG after reflector discussion]</i></p>

<p><i>[2019-11 Status to Ready after LWG discussion Friday in Belfast.]</i></p>



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

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

<blockquote>
<pre>
namespace std::ranges {
[&hellip;]
  <del>template&lt;class T, class U, class V&gt;
    concept <i>pair-like-convertible-to</i> = <i>// exposition only</i>
      !range&lt;T&gt; &amp;&amp; <i>pair-like</i>&lt;remove_reference_t&lt;T&gt;&gt; &amp;&amp;
      requires(T&amp;&amp; t) {
        { get&lt;0&gt;(std::forward&lt;T&gt;(t)) } -&gt; convertible_to&lt;U&gt;;
        { get&lt;1&gt;(std::forward&lt;T&gt;(t)) } -&gt; convertible_to&lt;V&gt;;
      };</del>
[&hellip;]
  template&lt;input_or_output_iterator I, sentinel_for&lt;I&gt; S = I, subrange_kind K =
           sized_sentinel_for&lt;S, I&gt; ? subrange_kind::sized : subrange_kind::unsized&gt;
    requires (K == subrange_kind::sized || !sized_sentinel_for&lt;S, I&gt;)
  class subrange : public view_interface&lt;subrange&lt;I, S, K&gt;&gt; {
  private:
    [&hellip;]
  public:
    [&hellip;]
    <del>template&lt;<i>not-same-as</i>&lt;subrange&gt; PairLike&gt;
      requires <i>pair-like-convertible-to</i>&lt;PairLike, I, S&gt;
    constexpr subrange(PairLike&amp;&amp; r) requires (!StoreSize)
      : subrange{std::get&lt;0&gt;(std::forward&lt;PairLike&gt;(r)),
                 std::get&lt;1&gt;(std::forward&lt;PairLike&gt;(r))}
    {}
    template&lt;<i>pair-like-convertible-to</i>&lt;I, S&gt; PairLike&gt;
    constexpr subrange(PairLike&amp;&amp; r, <i>make-unsigned-like-t</i>(iter_difference_t&lt;I&gt;) n)
      requires (K == subrange_kind::sized)
      : subrange{std::get&lt;0&gt;(std::forward&lt;PairLike&gt;(r)),
                 std::get&lt;1&gt;(std::forward&lt;PairLike&gt;(r)), n}
    {}</del>
  [&hellip;]
  };
[&hellip;]
}
</pre>
</blockquote>
</li>

</ol>





</body>
</html>
