<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 876: basic_string access operations should give stronger guarantees</title>
<meta property="og:title" content="Issue 876: basic_string access operations should give stronger guarantees">
<meta property="og:description" content="C++ library issue. Status: C++11">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue876.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#C++11">C++11</a> status.</em></p>
<h3 id="876"><a href="lwg-defects.html#876">876</a>. <code>basic_string</code> access operations should give stronger guarantees</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#C++11">C++11</a>
 <b>Submitter:</b> Daniel Kr&uuml;gler <b>Opened:</b> 2008-08-22 <b>Last modified:</b> 2016-01-28</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#C++11">C++11</a> status.</p>
<p><b>Discussion:</b></p>
<p>
During the Sophia Antipolis meeting it was decided to split-off some
parts of the
<a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2647.html">n2647</a>
("Concurrency modifications for <code>basic_string</code>")
proposal into a separate issue, because these weren't actually
concurrency-related. The here proposed changes refer to the recent
update document
<a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2668.htm">n2668</a>
and attempt to take advantage of the
stricter structural requirements.
</p>
<p>
Indeed there exists some leeway for more guarantees that would be
very useful for programmers, especially if interaction with transactionary
or exception-unaware C API code is important. This would also allow
compilers to take advantage of more performance optimizations, because
more functions can have throw() specifications. This proposal uses the
form of "Throws: Nothing" clauses to reach the same effect, because
there already exists a different issue in progress to clean-up the current
existing "schizophrenia" of the standard in this regard.
</p>
<p>
Due to earlier support for copy-on-write, we find the following
unnecessary limitations for C++0x:
</p>

<ol>
<li>
Missing no-throw guarantees: <code>data()</code> and <code>c_str()</code> simply return
a pointer to their guts, which is a non-failure operation. This should
be spelled out. It is also noteworthy to mention that the same
guarantees should also be given by the size query functions,
because the combination of pointer to content and the length is
typically needed during interaction with low-level API.
</li>
<li>
Missing complexity guarantees: <code>data()</code> and <code>c_str()</code> simply return
a pointer to their guts, which is guaranteed O(1). This should be
spelled out.
</li>
<li>
Missing reading access to the terminating character: Only the
const overload of <code>operator[]</code> allows reading access to the terminator
char. For more intuitive usage of strings, reading access to this
position should be extended to the non-const case. In contrast
to C++03 this reading access should now be homogeneously
an lvalue access.
</li>
</ol>

<p>
The proposed resolution is split into a main part (A) and a
secondary part (B) (earlier called "Adjunct Adjunct Proposal").
(B) extends (A) by also making access to index position
size() of the at() overloads a no-throw operation. This was
separated, because this part is theoretically observable in
specifically designed test programs.
</p>

<p><i>[
San Francisco:
]</i></p>


<blockquote>
<p>
We oppose part 1 of the issue but hope to address <code>size()</code> in
issue <a href="lwg-closed.html#877" title="to throw() or to Throw: Nothing. (Status: NAD)">877</a><sup><a href="https://cplusplus.github.io/LWG/issue877" title="Latest snapshot">(i)</a></sup>.
</p>
<p>
We do not support part B. 4 of the issue because of the breaking API change.
</p>
<p>
We support part A. 2 of the issue.
</p>
<p>
On support part A. 3 of the issue:
</p>
<blockquote><p>
Pete's broader comment: now that we know that <code>basic_string</code> will be a
block of contiguous memory, we should just rewrite its specification
with that in mind. The expression of the specification will be simpler
and probably more correct as a result.
</p></blockquote>
</blockquote>

<p><i>[
2009-07 Frankfurt
]</i></p>


<blockquote>
<p>
Move proposed resolution A to Ready.
</p>
<p><i>[
Howard: Commented out part B.
]</i></p>

</blockquote>



<p id="res-876"><b>Proposed resolution:</b></p>
<ol style="list-style-type:upper-alpha">
<li>
<ol>
<li>
<p>In 27.4.3.5 <a href="https://wg21.link/string.capacity">[string.capacity]</a>, just after p. 1 add a new paragraph:
</p>
<blockquote><p>
<i>Throws:</i> Nothing.
</p></blockquote>

</li>
<li>
<p>
In 27.4.3.6 <a href="https://wg21.link/string.access">[string.access]</a> <em>replace</em> p. 1 by the following <em>4</em> paragraghs:
</p>

<blockquote>
<p>
<i>Requires:</i> <code>pos &le; size()</code>.
</p>
<p>
<i>Returns:</i> If <code>pos &lt; size()</code>, returns <code>*(begin() + pos)</code>. Otherwise, returns
a reference to a <code>charT()</code> that shall not be modified.
</p>
<p>
<i>Throws:</i> Nothing.
</p>
<p>
<i>Complexity:</i> Constant time.
</p>
</blockquote>

</li>
<li>
<p>
In 27.4.3.8.1 <a href="https://wg21.link/string.accessors">[string.accessors]</a> replace the now <em>common</em> returns
clause of <code>c_str()</code> and <code>data()</code> by the following <em>three</em> paragraphs:
</p>
<blockquote>
<p>
<i>Returns:</i> A pointer <code>p</code> such that <code>p+i == &amp;operator[](i)</code> for each <code>i</code>
in <code>[0, size()]</code>.
</p>
<p>
<i>Throws:</i> Nothing.
</p>
<p>
<i>Complexity:</i> Constant time.
</p>
</blockquote>
</li>
</ol>
</li>

</ol>






</body>
</html>
