<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 180: Container member iterator arguments constness has unintended consequences</title>
<meta property="og:title" content="Issue 180: Container member iterator arguments constness has unintended consequences">
<meta property="og:description" content="C++ library issue. Status: CD1">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue180.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#CD1">CD1</a> status.</em></p>
<h3 id="180"><a href="lwg-defects.html#180">180</a>. Container member iterator arguments constness has unintended consequences</h3>
<p><b>Section:</b> 27.4.3 <a href="https://wg21.link/basic.string">[basic.string]</a> <b>Status:</b> <a href="lwg-active.html#CD1">CD1</a>
 <b>Submitter:</b> Dave Abrahams <b>Opened:</b> 1999-07-01 <b>Last modified:</b> 2016-11-12</p>
<p><b>Priority: </b>Not Prioritized
</p>
<p><b>View other</b> <a href="lwg-index-open.html#basic.string">active issues</a> in [basic.string].</p>
<p><b>View all other</b> <a href="lwg-index.html#basic.string">issues</a> in [basic.string].</p>
<p><b>View all issues with</b> <a href="lwg-status.html#CD1">CD1</a> status.</p>
<p><b>Discussion:</b></p>
<p>It is the constness of the container which should control whether
it can be modified through a member function such as erase(), not the
constness of the iterators. The iterators only serve to give
positioning information.</p>

<p>Here's a simple and typical example problem which is currently very
difficult or impossible to solve without the change proposed
below.</p>

<p>Wrap a standard container C in a class W which allows clients to
find and read (but not modify) a subrange of (C.begin(), C.end()]. The
only modification clients are allowed to make to elements in this
subrange is to erase them from C through the use of a member function
of W.</p>

<p><i>[
post Bellevue, Alisdair adds:
]</i></p>


<blockquote>
<p>
This issue was implemented by
<a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2350.pdf">N2350</a>
for everything but <code>basic_string</code>.
</p>

<p>
Note that the specific example in this issue (<code>basic_string</code>) is the one place
we forgot to amend in
<a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2350.pdf">N2350</a>,
so we might open this issue for that
single container?
</p>
</blockquote>

<p><i>[
Sophia Antipolis:
]</i></p>


<blockquote>
<p>
This was a fix that was intended for all standard library containers,
and has been done for other containers, but string was missed.
</p>
<p>
The wording updated.
</p>
<p>
We did not make the change in <code>replace</code>, because this change would affect
the implementation because the string may be written into. This is an
issue that should be taken up by concepts.
</p>
<p>
We note that the supplied wording addresses the initializer list provided in
<a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2679.pdf">N2679</a>.
</p>
</blockquote>



<p id="res-180"><b>Proposed resolution:</b></p>
<p>
Update the following signature in the <code>basic_string</code> class template definition in
27.4.3 <a href="https://wg21.link/basic.string">[basic.string]</a>, p5:
</p>
<blockquote><pre>
namespace std {
  template&lt;class charT, class traits = char_traits&lt;charT&gt;,
    class Allocator = allocator&lt;charT&gt; &gt;
  class basic_string {

    ...

    iterator insert(<ins>const_</ins>iterator p, charT c);
    void insert(<ins>const_</ins>iterator p, size_type n, charT c);
    template&lt;class InputIterator&gt;
      void insert(<ins>const_</ins>iterator p, InputIterator first, InputIterator last);
    void insert(<ins>const_</ins>iterator <ins>p</ins>, initializer_list&lt;charT&gt;);

    ...

    iterator erase(<ins>const_</ins>iterator <ins>const_</ins>position);
    iterator erase(<ins>const_</ins>iterator first, <ins>const_</ins>iterator last);

    ...

  };
}
</pre></blockquote>

<p>
Update the following signatures in 27.4.3.7.4 <a href="https://wg21.link/string.insert">[string.insert]</a>:
</p>

<blockquote><pre>
iterator insert(<ins>const_</ins>iterator p, charT c);
void insert(<ins>const_</ins>iterator p, size_type n, charT c);
template&lt;class InputIterator&gt;
  void insert(<ins>const_</ins>iterator p, InputIterator first, InputIterator last);
void insert(<ins>const_</ins>iterator <ins>p</ins>, initializer_list&lt;charT&gt;);
</pre></blockquote>

<p>
Update the following signatures in 27.4.3.7.5 <a href="https://wg21.link/string.erase">[string.erase]</a>:
</p>

<blockquote><pre>
iterator erase(<ins>const_</ins>iterator <ins>const_</ins>position);
iterator erase(<ins>const_</ins>iterator first, <ins>const_</ins>iterator last);
</pre></blockquote>



<p><b>Rationale:</b></p>
<p>The issue was discussed at length. It was generally agreed that 1)
There is no major technical argument against the change (although
there is a minor argument that some obscure programs may break), and
2) Such a change would not break const correctness. The concerns about
making the change were 1) it is user detectable (although only in
boundary cases), 2) it changes a large number of signatures, and 3) it
seems more of a design issue that an out-and-out defect.</p>

<p>The LWG believes that this issue should be considered as part of a
general review of const issues for the next revision of the
standard. Also see issue <a href="lwg-defects.html#200" title="Forward iterator requirements don't allow constant iterators (Status: CD1)">200</a><sup><a href="https://cplusplus.github.io/LWG/issue200" title="Latest snapshot">(i)</a></sup>.</p>




</body>
</html>
