<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 3992: basic_stringbuf::str()&amp;&amp; should enforce &#x1d4aa;(1)</title>
<meta property="og:title" content="Issue 3992: basic_stringbuf::str()&amp;&amp; should enforce &#x1d4aa;(1)">
<meta property="og:description" content="C++ library issue. Status: New">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue3992.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="3992"><a href="lwg-active.html#3992">3992</a>. <code>basic_stringbuf::str()&amp;&amp;</code> should enforce &#x1d4aa;(1)</h3>
<p><b>Section:</b> 31.8.2.4 <a href="https://wg21.link/stringbuf.members">[stringbuf.members]</a> <b>Status:</b> <a href="lwg-active.html#New">New</a>
 <b>Submitter:</b> Peter Sommerlad <b>Opened:</b> 2023-10-05 <b>Last modified:</b> 2023-10-14</p>
<p><b>Priority: </b>Not Prioritized
</p>
<p><b>View all other</b> <a href="lwg-index.html#stringbuf.members">issues</a> in [stringbuf.members].</p>
<p><b>View all issues with</b> <a href="lwg-status.html#New">New</a> status.</p>
<p><b>Discussion:</b></p>
<p>
Recent discussions on <a href="https://github.com/llvm/llvm-project/issues/64644">llvm-64644</a>
came to the conclusion that <code>basic_stringbuf() &amp;&amp;</code> introduced by <a href="https://wg21.link/P0408" title=" Efficient Access to basic_stringbuf’s Buffer">P0408</a>
might just copy the underlying buffer into a string object and not actually move the allocated space. 
While the wording tried to encourage that, especially with the postcondition that the buffer must 
be empty afterwards, it failed to specify that the move is never a copy.
<p/>
I suggest to amend the specification to enforce implementors to do the <code>&#x1d4aa;(1)</code> thing. 
There might be ABI issues for those who still copy.
<p/>
Some investigation into 23.2.2.2 <a href="https://wg21.link/container.reqmts">[container.reqmts]</a> p.16 and 27.4.3.1 <a href="https://wg21.link/basic.string.general">[basic.string.general]</a> 
shows that a <code>basic_string</code> as a standard container should move with <code>&#x1d4aa;(1)</code>.
<p/>
Unfortunately, we cannot say
</p>
<blockquote><p>
<ins><code>str().data() == buf.data()</code> before calling <code>str()</code></ins>
</p></blockquote>
<p>
as a postcondition due to SSO. Maybe a note could be added to eliminate the confusion.
</p>


<p id="res-3992"><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 31.8.2.4 <a href="https://wg21.link/stringbuf.members">[stringbuf.members]</a> as indicated:</p>

<blockquote>
<pre>
basic_string&lt;charT, traits, Allocator&gt; str() &amp;&amp;;
</pre>
<blockquote>
<p>
-9- <i>Postconditions</i>: The underlying character sequence <code>buf</code> is empty and <code>pbase()</code>, <code>pptr()</code>, 
<code>epptr()</code>, <code>eback()</code>, <code>gptr()</code>, and <code>egptr()</code> are initialized as if by calling 
<code>init_buf_ptrs()</code> with an empty <code>buf</code>.
<p/>
-10- <i>Returns</i>: A <code>basic_string&lt;charT, traits, Allocator&gt;</code> object move constructed from the 
<code>basic_stringbuf</code>'s underlying character sequence in <code>buf</code>. This can be achieved by first adjusting <code>buf</code> 
to have the same content as <code>view()</code>.
<p/>
<ins>[<i>Note:</i> &mdash; 23.2.2.2 <a href="https://wg21.link/container.reqmts">[container.reqmts]</a> require the move construction of the return
value to be <code>&#x1d4aa;(1)</code> <i>end note</i>]</ins>
</p>
</blockquote>
</blockquote>

</li>

</ol>





</body>
</html>
