<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 4029: basic_string accidentally fails to meet the reversible container requirements</title>
<meta property="og:title" content="Issue 4029: basic_string accidentally fails to meet the reversible container requirements">
<meta property="og:description" content="C++ library issue. Status: New">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue4029.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="4029"><a href="lwg-active.html#4029">4029</a>. <code>basic_string</code> accidentally fails to meet the reversible container requirements</h3>
<p><b>Section:</b> 27.4.3.1 <a href="https://wg21.link/basic.string.general">[basic.string.general]</a> <b>Status:</b> <a href="lwg-active.html#New">New</a>
 <b>Submitter:</b> Jan Schultke <b>Opened:</b> 2023-12-18 <b>Last modified:</b> 2024-03-15</p>
<p><b>Priority: </b>3
</p>
<p><b>View all other</b> <a href="lwg-index.html#basic.string.general">issues</a> in [basic.string.general].</p>
<p><b>View all issues with</b> <a href="lwg-status.html#New">New</a> status.</p>
<p><b>Discussion:</b></p>
<p>
The complexity requirements for a reversible container (23.2.2.3 <a href="https://wg21.link/container.rev.reqmts">[container.rev.reqmts]</a>) are that each 
function must have constant complexity. The corresponding member functions in 27.4.3.4 <a href="https://wg21.link/string.iterators">[string.iterators]</a> 
have no complexity requirements, and <code>basic_string</code> unintentionally is not a reversible container 
(unless the implementation coincidentally provides constant complexity member functions).
</p>

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

<p>
Set priority to 3 after reflector poll.
</p>
<p>Could harmonize with <code>vector</code> by saying:
"A basic_string meets all of the requirements of a contiguous container
(23.2.2.2 <a href="https://wg21.link/container.reqmts">[container.reqmts]</a>) and of a reversible container
(23.2.2.3 <a href="https://wg21.link/container.rev.reqmts">[container.rev.reqmts]</a>)."
It's not a sequence container (see LWG <a href="lwg-closed.html#718" title="basic_string is not a sequence (Status: NAD Editorial)">718</a><sup><a href="https://cplusplus.github.io/LWG/issue718" title="Latest snapshot">(i)</a></sup>)
but as per 23.2.2.5 <a href="https://wg21.link/container.alloc.reqmts">[container.alloc.reqmts]</a> p1 it is allocator-aware
(although it doesn't use <code>construct</code> and <code>destroy</code>).
</p>
<p>
Not comfortable removing the detailed descriptions for those members.
We don't have the "Descriptions are only provided here for operations on [...]
that are not described in one of these tables" wording that the containers have.
</p>



<p id="res-4029"><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>

<blockquote class="note">
<p>
[<i>Drafting Note:</i> The proposed wording is similar to the specification in 23.3.13.1 <a href="https://wg21.link/vector.overview">[vector.overview]</a> p2 
and suggests to simply strike 27.4.3.4 <a href="https://wg21.link/string.iterators">[string.iterators]</a> because it doesn't say anything new compared to 
23.2.2.2 <a href="https://wg21.link/container.reqmts">[container.reqmts]</a> and 23.2.2.3 <a href="https://wg21.link/container.rev.reqmts">[container.rev.reqmts]</a>.
<p/>
Alternatively, one could add a
<p/>
<ins><i>Complexity</i>: Constant.</ins>
<p/>
paragraph to each function in 27.4.3.4 <a href="https://wg21.link/string.iterators">[string.iterators]</a>, but that would be less clearer and would not
explicitly say <code>basic_string</code> meets the reversible container requirements.
] 
</p>
</blockquote>

<ol>

<li><p>Modify 27.4.3.1 <a href="https://wg21.link/basic.string.general">[basic.string.general]</a> as indicated:</p>

<blockquote>
<p>
-2- A specialization of <code>basic_string</code> is a contiguous container (23.2.2.2 <a href="https://wg21.link/container.reqmts">[container.reqmts]</a>) <ins>and a
reversible container (23.2.2.3 <a href="https://wg21.link/container.rev.reqmts">[container.rev.reqmts]</a>)</ins>.
<p/>
-3- In all cases, <code>[data(), data() + size()]</code> is a valid range, <code>data() + size()</code> points at 
an object with value <code>charT()</code> (a "null terminator"), and <code>size() &lt;= capacity()</code> is <code>true</code>.
</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 {
    [&hellip;]
    // <i><del>27.4.3.4 <a href="https://wg21.link/string.iterators">[string.iterators]</a>,</del> iterators</i>
    constexpr iterator begin() noexcept;
    constexpr const_iterator begin() const noexcept;
    constexpr iterator end() noexcept;
    constexpr const_iterator end() const noexcept;
  
    constexpr reverse_iterator rbegin() noexcept;
    constexpr const_reverse_iterator rbegin() const noexcept;
    constexpr reverse_iterator rend() noexcept;
    constexpr const_reverse_iterator rend() const noexcept;
  
    constexpr const_iterator cbegin() const noexcept;
    constexpr const_iterator cend() const noexcept;
    constexpr const_reverse_iterator crbegin() const noexcept;
    constexpr const_reverse_iterator crend() const noexcept;
  
    // <i>27.4.3.5 <a href="https://wg21.link/string.capacity">[string.capacity]</a>, capacity</i>
    [&hellip;]
  };
  [&hellip;]
}
</pre></blockquote>
[&hellip;]
</blockquote>
</li>

<li><p>Remove subclause 27.4.3.4 <a href="https://wg21.link/string.iterators">[string.iterators]</a> in its entirety:</p>

<blockquote>
<p>
<del><b>23.4.3.4 Iterator support [string.iterators]</b></del>
</p>
<pre>
<del>constexpr iterator begin() noexcept;
constexpr const_iterator begin() const noexcept;
constexpr const_iterator cbegin() const noexcept;</del>
</pre>
<blockquote>
<p>
<del>-1- <i>Returns</i>: An iterator referring to the first character in the string.</del>
</p>
</blockquote>
[&hellip;]
<pre>
<del>constexpr reverse_iterator rend() noexcept;
constexpr const_reverse_iterator rend() const noexcept;
constexpr const_reverse_iterator crend() const noexcept;</del>
</pre>
<blockquote>
<p>
<del>-4- <i>Returns</i>: An iterator which is semantically equivalent to <code>reverse_iterator(begin())</code>.</del>
</p>
</blockquote>
</blockquote>

</li>

</ol>







</body>
</html>
