<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 4027: possibly-const-range should prefer returning const R&amp;</title>
<meta property="og:title" content="Issue 4027: possibly-const-range should prefer returning const R&amp;">
<meta property="og:description" content="C++ library issue. Status: WP">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue4027.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#WP">WP</a> status.</em></p>
<h3 id="4027"><a href="lwg-defects.html#4027">4027</a>. <code><i>possibly-const-range</i></code> should prefer returning <code>const R&amp;</code></h3>
<p><b>Section:</b> 25.2 <a href="https://wg21.link/ranges.syn">[ranges.syn]</a> <b>Status:</b> <a href="lwg-active.html#WP">WP</a>
 <b>Submitter:</b> Hewill Kang <b>Opened:</b> 2023-12-17 <b>Last modified:</b> 2024-11-28</p>
<p><b>Priority: </b>2
</p>
<p><b>View other</b> <a href="lwg-index-open.html#ranges.syn">active issues</a> in [ranges.syn].</p>
<p><b>View all other</b> <a href="lwg-index.html#ranges.syn">issues</a> in [ranges.syn].</p>
<p><b>View all issues with</b> <a href="lwg-status.html#WP">WP</a> status.</p>
<p><b>Discussion:</b></p>
<p>
<code><i>possibly-const-range</i></code> currently only returns <code>const R&amp;</code> when <code>R</code> does not 
satisfy <code>constant_range</code> and <code>const R</code> satisfies <code>constant_range</code>.
<p/>
Although it's not clear why we need the former condition, this does diverge from the legacy <code>std::cbegin</code> 
(<a href="https://godbolt.org/z/636osY7os">demo</a>):
</p>
<blockquote><pre>
#include &lt;ranges&gt;

int main() {
  auto r = std::views::single(0)
        | std::views::transform([](int) { return 0; });
  using C1 = decltype(std::ranges::cbegin(r));
  using C2 = decltype(std::cbegin(r));
  static_assert(std::same_as&lt;C1, C2&gt;); // <span style="color:red;font-weight:bolder">failed</span>
}
</pre></blockquote>
<p>
Since <code>R</code> itself is <code>constant_range</code>, so <code><i>possibly-const-range</i></code>, above just returns 
<code>R&amp;</code> and <code>C1</code> is <code>transform_view::<i>iterator</i>&lt;false&gt;</code>; <code>std::cbegin</code> 
specifies to return <code>as_const(r).begin()</code>, which makes that <code>C2</code> is 
<code>transform_view::<i>iterator</i>&lt;true&gt;</code> which is different from <code>C1</code>.
<p/>
I believe <code>const R&amp;</code> should always be returned if it's a range, regardless of whether <code>const R</code> 
or <code>R</code> is a <code>constant_range</code>, just as <code><i>fmt-maybe-const</i></code> in format ranges always prefers 
<code>const R</code> over <code>R</code>.
<p/>
Although it is theoretically possible for <code>R</code> to satisfy <code>constant_range</code> and that <code>const R</code> 
is a mutable range, such nonsense range type should not be of interest.
<p/>
This relaxation of constraints allows for maximum consistency with <code>std::cbegin</code>, and in some cases can 
preserve constness to the greatest extent (<a href="https://godbolt.org/z/3hYToMq35">demo</a>):
</p>
<blockquote><pre>
#include &lt;ranges&gt;

int main() {
  auto r = std::views::single(0) | std::views::lazy_split(0);
  (*std::ranges::cbegin(r)).front() = 42; // ok
  (*std::cbegin(r)).front() = 42; // <span style="color:red;font-weight:bolder">not ok</span>
}
</pre></blockquote>
<p>
Above, <code>*std::ranges::cbegin</code> returns a range of type <code>const lazy_split_view::<i>outer-iterator</i>&lt;false&gt;::value_type</code>, 
which does not satisfy <code>constant_range</code> because its reference type is <code>int&amp;</code>.
<p/>
However, <code>*std::cbegin(r)</code> returns <code>lazy_split_view::<i>outer-iterator</i>&lt;true&gt;::value_type</code> 
whose reference type is <code>const int&amp;</code> and satisfies <code>constant_range</code>.
</p>

<p><i>[2024-03-11; Reflector poll]</i></p>

<p>
Set priority to 2 after reflector poll. Send to SG9.
</p>

<p><i>[St. Louis 2024-06-28; LWG and SG9 joint session: move to Ready]</i></p>


<p><i>[Wrocław 2024-11-23; Status changed: Voting &rarr; WP.]</i></p>



<p id="res-4027"><b>Proposed resolution:</b></p>
<p>
This wording is relative to <a href="https://wg21.link/N4971" title=" Working Draft, Programming Languages — C++">N4971</a>.
</p>

<ol>

<li><p>Modify 25.2 <a href="https://wg21.link/ranges.syn">[ranges.syn]</a>, header <code>&lt;ranges&gt;</code> synopsis, as indicated:</p>

<blockquote>
<pre>
#include &lt;compare&gt;              // <i>see 17.12.1 <a href="https://wg21.link/compare.syn">[compare.syn]</a></i>
#include &lt;initializer_list&gt;     // <i>see 17.11.2 <a href="https://wg21.link/initializer.list.syn">[initializer.list.syn]</a></i>
#include &lt;iterator&gt;             // <i>see 24.2 <a href="https://wg21.link/iterator.synopsis">[iterator.synopsis]</a></i>

namespace std::ranges {
  [&hellip;]

  // <i>25.7.22 <a href="https://wg21.link/range.as.const">[range.as.const]</a>, as const view</i>
  template&lt;input_range R&gt;
    constexpr auto&amp; <i>possibly-const-range</i>(R&amp; r) noexcept { // <i>exposition only</i>
      if constexpr (<ins>input</ins><del>constant</del>_range&lt;const R&gt;<del> &amp;&amp; !constant_range&lt;R&gt;</del>) {
        return const_cast&lt;const R&amp;&gt;(r);
      } else {
        return r;
      }
    }

  [&hellip;]
}
</pre>
</blockquote>
</li>

</ol>







</body>
</html>
