<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 2994: Needless UB for basic_string and basic_string_view</title>
<meta property="og:title" content="Issue 2994: Needless UB for basic_string and basic_string_view">
<meta property="og:description" content="C++ library issue. Status: WP">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue2994.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="2994"><a href="lwg-defects.html#2994">2994</a>. Needless UB for <code>basic_string</code> and <code>basic_string_view</code></h3>
<p><b>Section:</b> 27.2 <a href="https://wg21.link/char.traits">[char.traits]</a>, 27.4.3.2 <a href="https://wg21.link/string.require">[string.require]</a>, 27.3.3 <a href="https://wg21.link/string.view.template">[string.view.template]</a> <b>Status:</b> <a href="lwg-active.html#WP">WP</a>
 <b>Submitter:</b> Gennaro Prota <b>Opened:</b> 2017-07-03 <b>Last modified:</b> 2023-11-22</p>
<p><b>Priority: </b>3
</p>
<p><b>View all other</b> <a href="lwg-index.html#char.traits">issues</a> in [char.traits].</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>basic_string</code> and <code>basic_string_view</code> involve undefined behavior in a few cases where it's 
simple for the implementation to add a <code>static_assert</code> and make the program ill-formed.
<p/>
With regards to basic_string, 27.2 <a href="https://wg21.link/char.traits">[char.traits]</a>/3 states:
</p>
<blockquote><p>
<code>Traits::char_type</code> shall be the same as <code>CharT</code>.
</p></blockquote>
<p>
Here, the implementation can add a <code>static_assert</code> using the <code>is_same</code>
type trait. Similar issues exist in 27.4.3.2 <a href="https://wg21.link/string.require">[string.require]</a> and, for
<code>basic_string_view</code>, in 27.3.3 <a href="https://wg21.link/string.view.template">[string.view.template]</a>/1. 
</p>

<p><i>[2017-07 Toronto Tuesday PM issue prioritization]</i></p>

<p>Priority 3; need to check general container requirements</p>

<p>Partially by the adoption of <a href="https://wg21.link/P1148">P1148</a> in San Diego.</p>
<p>Tim opines: "the remainder deals with allocator value type mismatch, which I think is NAD."</p>
<p><strong>Previous resolution [SUPERSEDED]:</strong></p>
<blockquote class="note">

<p>
This wording is relative to <a href="https://wg21.link/n4659">N4659</a>.
</p>

<ol>
<li><p>Edit 27.2 <a href="https://wg21.link/char.traits">[char.traits]</a> as indicated:</p>

<blockquote>
<p>
-3- To specialize those templates to generate a string or iostream class to handle a particular character container
type <code>CharT</code>, that and its related character traits class <code>Traits</code> are passed as a pair of parameters to 
the string or iostream template as parameters <code>charT</code> and <code>traits</code>. <ins>If</ins> <code>Traits::char_type</code> 
<del>shall be the same</del> <ins>is not the same type</ins> as <code>CharT</code><ins>, the program is ill-formed</ins>.
</p>
</blockquote>
</li>

<li><p>Edit 27.4.3.2 <a href="https://wg21.link/string.require">[string.require]</a> as indicated:</p>

<blockquote>
<p>
-3- In every specialization <code>basic_string&lt;charT, traits, Allocator&gt;</code>, <ins>if</ins> <del>the type</del> 
<code>allocator_traits&lt;Allocator&gt;::value_type</code> <del>shall name the same type</del> <ins>is not the 
same type</ins> as <code>charT</code><ins>, the program is ill-formed</ins>. 
Every object of type <code>basic_string&lt;charT, traits, Allocator&gt;</code> shall use an object of type 
<code>Allocator</code> to allocate and free storage for the contained <code>charT</code> objects as needed. The 
<code>Allocator</code> object used shall be obtained as described in 23.2.2 <a href="https://wg21.link/container.requirements.general">[container.requirements.general]</a>. 
In every specialization <code>basic_string&lt;charT, traits, Allocator&gt;</code>, the type <code>traits</code> 
shall satisfy the character traits requirements (27.2 <a href="https://wg21.link/char.traits">[char.traits]</a>)<ins>. If</ins><del>, and the type</del> 
<code>traits::char_type</code> <del>shall name the same type</del> <ins>is not the same type</ins> as <code>charT</code><ins>, 
the program is ill-formed</ins>.
</p>
</blockquote>
</li>

<li><p>Edit 27.3.3 <a href="https://wg21.link/string.view.template">[string.view.template]</a> as indicated:</p>

<blockquote>
<p>
-1- In every specialization <code>basic_string_view&lt;charT, traits&gt;</code>, the type <code>traits</code> shall satisfy the character
traits requirements (27.2 <a href="https://wg21.link/char.traits">[char.traits]</a>)<ins>. If</ins><del>, and the type</del> <code>traits::char_type</code> <del>shall 
name the same type</del> <ins>is not the same type</ins> as <code>charT</code><ins>, the program is ill-formed</ins>.
</p>
</blockquote>
</li>

</ol>
</blockquote>

<p><i>[2023-05-15; Jonathan Wakely provides improved wording]</i></p>

<p>
As noted above, most of this issue was resolved by <a href="https://wg21.link/P1148R0" title=" Cleaning up Clause 20">P1148R0</a>.
The remainder is the change to make it ill-formed if the allocator has the wrong
<code>value_type</code>, but since <a href="https://wg21.link/P1463R1" title=" Mandating the Standard Library: Clause 21 - Containers library">P1463R1</a> that is already part of
the Allocator-aware container requirements, which apply to <code>basic_string</code>.
</p>


<p><i>[2023-06-01; Reflector poll]</i></p>

<p>
Set status to Tentatively Ready after eight votes in favour during reflector poll.
</p>
<p>
There was a request to editorially move the added text in <i>Note 1</i> into
<i>Note 2</i> after this is approved.
</p>

<p><i>[2023-06-17 Approved at June 2023 meeting in Varna. Status changed: Voting &rarr; WP.]</i></p>



<p id="res-2994"><b>Proposed resolution:</b></p>
<p>
This wording is relative to <a href="https://wg21.link/N4950" title=" Working Draft, Standard for Programming Language C++">N4950</a>.
</p>

<ol>
<li><p>Edit 27.4.3.2 <a href="https://wg21.link/string.require">[string.require]</a> as indicated:</p>

<blockquote>
<p>
-3- <del>In every specialization <code>basic_string&lt;charT, traits, Allocator&gt;</code>, the type
<code>allocator_traits&lt;Allocator&gt;::value_type</code> shall name the same type as <code>charT</code>.</del>
Every object of type <code>basic_string&lt;charT, traits, Allocator&gt;</code> shall use an object of type
<code>Allocator</code> to allocate and free storage for the contained <code>charT</code> objects as needed. The
In every specialization <code>basic_string&lt;charT, traits, Allocator&gt;</code>, the type <code>traits</code>
shall satisfy the character traits requirements (27.2 <a href="https://wg21.link/char.traits">[char.traits]</a>).
</p>
<p>
[<i>Note 1</i>:
Every specialization <code>basic_string&lt;charT, traits, Allocator&gt;</code>
is an allocator-aware container, but does not use the allocator’s
<code>construct</code> and <code>destroy</code> member functions
(23.2.2 <a href="https://wg21.link/container.requirements.general">[container.requirements.general]</a>).
<ins>
The program is ill-formed if <code>Allocator::value_type</code>
is not the same type as <code>charT</code>.
</ins>
&mdash; <i>end note</i>]
</p>
<p>
[<i>Note 2</i>:
The program is ill-formed if <code>traits::char_type</code>
is not the same type as <code>charT</code>.
&mdash; <i>end note</i>]
</p>
</blockquote>
</li>
</ol>





</body>
</html>
