<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 3988: Should as_const_view and basic_const_iterator provide base()?</title>
<meta property="og:title" content="Issue 3988: Should as_const_view and basic_const_iterator provide base()?">
<meta property="og:description" content="C++ library issue. Status: Open">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue3988.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#Open">Open</a> status.</em></p>
<h3 id="3988"><a href="lwg-active.html#3988">3988</a>. Should <code>as_const_view</code> and <code>basic_const_iterator</code> provide <code>base()</code>?</h3>
<p><b>Section:</b> 24.5.3 <a href="https://wg21.link/const.iterators">[const.iterators]</a>, 25.7.22.2 <a href="https://wg21.link/range.as.const.view">[range.as.const.view]</a> <b>Status:</b> <a href="lwg-active.html#Open">Open</a>
 <b>Submitter:</b> Hewill Kang <b>Opened:</b> 2023-08-28 <b>Last modified:</b> 2024-03-15</p>
<p><b>Priority: </b>3
</p>
<p><b>View other</b> <a href="lwg-index-open.html#const.iterators">active issues</a> in [const.iterators].</p>
<p><b>View all other</b> <a href="lwg-index.html#const.iterators">issues</a> in [const.iterators].</p>
<p><b>View all issues with</b> <a href="lwg-status.html#Open">Open</a> status.</p>
<p><b>Discussion:</b></p>
<p>
Currently, both <code>as_const_view</code> and <code>basic_const_iterator</code> provide <code>base()</code> members to 
return the underlying range and iterator, which seems to expose vulnerabilities in modifying them:
</p>
<blockquote><pre>
#include &lt;vector&gt;
#include &lt;ranges&gt;

int main() {
  std::vector v{1, 2, 3};
  
  auto f = [](std::span&lt;int&gt;::const_iterator i) {
    *i.base() = 4;
  };
  f(std::span{v}.cbegin());

  auto g = [](const std::ranges::constant_range auto&amp; r) {
    r.begin().base()[1] = 5;
    r.base()[2] = 6;
  };
  g(std::ranges::as_const_view(v));

  // v now becomes [4, 5, 6]
}
</pre></blockquote>
<p>
I don't think such a shortcut should be provided as it doesn't seem to be the intention and could be harmful.
</p>

<p><i>[2023-10-30; Reflector poll]</i></p>

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

<p><i>[Kona 2023-11-07; move to Ready]</i></p>


<p><i>[2024-03-15; move back to Open following LEWG feedback]</i></p>


<p>
SG9 approved the proposed change but then LEWG had no consensus for change.
LWG should revisit in Tokyo.
</p>



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

<ol>

<li><p>Modify 24.5.3.3 <a href="https://wg21.link/const.iterators.iterator">[const.iterators.iterator]</a>, class template <code>basic_const_iterator</code> synopsis, as indicated:</p>

<blockquote>
<pre>
namespace std {
  [&hellip;]

  template&lt;input_iterator Iterator&gt;
  class basic_const_iterator {
    [&hellip;]
    <del>constexpr const Iterator&amp; base() const &amp; noexcept;</del>
    <del>constexpr Iterator base() &amp;&amp;;</del>
    [&hellip;]
  };
}
</pre>
</blockquote>

</li>

<li><p>Modify 24.5.3.5 <a href="https://wg21.link/const.iterators.ops">[const.iterators.ops]</a> as indicated:</p>

<blockquote>
<pre>
<del>constexpr const Iterator&amp; base() const &amp; noexcept;</del>
</pre>
<blockquote>
<p>
<del>-4- <i>Effects</i>: Equivalent to: <code>return <i>current_</i>;</code></del>
</p>
</blockquote>
<pre>
<del>constexpr Iterator base() &amp;&amp;;</del>
</pre>
<blockquote>
<p>
<del>-5- <i>Effects</i>: Equivalent to: <code>return std::move(<i>current_</i>);</code></del>
</p>
</blockquote>
</blockquote>

</li>

<li><p>Modify 25.7.22.2 <a href="https://wg21.link/range.as.const.view">[range.as.const.view]</a> as indicated:</p>

<blockquote>
<pre>
namespace std::ranges {
  template&lt;view V&gt;
    requires input_range&lt;V&gt;
  class as_const_view : public view_interface&lt;as_const_view&lt;V&gt;&gt; {
    [&hellip;]
    <del>constexpr V base() const &amp; requires copy_constructible&lt;V&gt; { return <i>base_</i>; }</del>
    <del>constexpr V base() &amp;&amp; { return std::move(<i>base_</i>); }</del>
    [&hellip;]
  };
}
</pre>
</blockquote>

</li>

</ol>





</body>
</html>
