<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 2064: More noexcept issues in basic_string</title>
<meta property="og:title" content="Issue 2064: More noexcept issues in basic_string">
<meta property="og:description" content="C++ library issue. Status: C++14">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue2064.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++14">C++14</a> status.</em></p>
<h3 id="2064"><a href="lwg-defects.html#2064">2064</a>. More <code>noexcept</code> issues in <code>basic_string</code></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++14">C++14</a>
 <b>Submitter:</b> Howard Hinnant <b>Opened:</b> 2011-05-29 <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#C++14">C++14</a> status.</p>
<p><b>Discussion:</b></p>
<p>
The following inconsistencies regarding <code>noexcept</code> for <code>basic_string</code> are noted.
<p/>
Member swap is not marked <code>noexcept</code>:
</p><blockquote><pre>
void swap(basic_string&amp; str);
</pre></blockquote><p>
But the global swap is marked <code>noexcept</code>:
</p><blockquote><pre>
template&lt;class charT, class traits, class Allocator&gt;
void swap(basic_string&lt;charT,traits,Allocator&gt;&amp; lhs,
          basic_string&lt;charT,traits,Allocator&gt;&amp; rhs) <span style="color:#C80000;font-weight:bolder">noexcept</span>;
</pre></blockquote><p>
But only in the definition, not in the synopsis.
<p/>
All comparison operators are marked <code>noexcept</code> in their definitions, but not in the synopsis.
<p/>
The compare function that takes a pointer:
</p><blockquote><pre>
int compare(const charT *s) const;
</pre></blockquote><p>
is not marked <code>noexcept</code>. But some of the comparison functions which are marked <code>noexcept</code> 
(only in their definition) are specified to call the throwing compare operator:
</p><blockquote><pre>
template&lt;class charT, class traits, class Allocator&gt;
bool operator==(const basic_string&lt;charT,traits,Allocator&gt;&amp; lhs,
                const charT* rhs) <span style="color:#C80000;font-weight:bolder">noexcept</span>;
</pre><blockquote><p>
<i>Returns</i>: <code>lhs.compare(rhs) == 0</code>.
</p></blockquote></blockquote>
<p>
All functions with a narrow contract should not be declared as <code>noexcept</code> according to
the guidelines presented in <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2011/n3279.pdf">n3279</a>.
Among these narrow contract functions are the <code>swap</code> functions (23.2.2 <a href="https://wg21.link/container.requirements.general">[container.requirements.general]</a> p. 8) 
and functions with non-<code>NULL</code> <code>const charT*</code> parameters.
</p>
<p><i>[2011-06-08 Daniel provides wording]</i></p>


<p><i>[Bloomington, 2011]</i></p>

<p>
Move to Ready
</p>



<p id="res-2064"><b>Proposed resolution:</b></p>
<p>This wording is relative to the FDIS. Both move-assignment operator and the moving <code>assign</code>
function are not touched by this issue, because they are handled separately by issue <a href="lwg-defects.html#2063" title="Contradictory requirements for string move assignment (Status: C++17)">2063</a><sup><a href="https://cplusplus.github.io/LWG/issue2063" title="Latest snapshot">(i)</a></sup>.</p>
<ol>
<li><p>Modify the header <code>&lt;string&gt;</code> synopsis in 27.4 <a href="https://wg21.link/string.classes">[string.classes]</a> as 
indicated (Rationale: Adding <code>noexcept</code> to these specific overloads is in sync with
applying the same rule to specific overloads of the member functions <code>find</code>, <code>compare</code>, etc.
This approach deviates from that taken in <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2011/n3279.pdf">n3279</a>,
but seems more consistent given similar application for comparable member functions):</p>

<blockquote><pre>
#include &lt;initializer_list&gt;

namespace std {

  [&hellip;]
  template&lt;class charT, class traits, class Allocator&gt;
    bool operator==(const basic_string&lt;charT,traits,Allocator&gt;&amp; lhs,
                    const basic_string&lt;charT,traits,Allocator&gt;&amp; rhs) <ins>noexcept</ins>;
  [&hellip;]
  template&lt;class charT, class traits, class Allocator&gt;
    bool operator!=(const basic_string&lt;charT,traits,Allocator&gt;&amp; lhs,
                    const basic_string&lt;charT,traits,Allocator&gt;&amp; rhs) <ins>noexcept</ins>;
  [&hellip;]

  template&lt;class charT, class traits, class Allocator&gt;
    bool operator&lt;(const basic_string&lt;charT,traits,Allocator&gt;&amp; lhs,
                   const basic_string&lt;charT,traits,Allocator&gt;&amp; rhs) <ins>noexcept</ins>;
  [&hellip;]
  template&lt;class charT, class traits, class Allocator&gt;
    bool operator&gt;(const basic_string&lt;charT,traits,Allocator&gt;&amp; lhs,
                   const basic_string&lt;charT,traits,Allocator&gt;&amp; rhs) <ins>noexcept</ins>;
  [&hellip;]

  template&lt;class charT, class traits, class Allocator&gt;
    bool operator&lt;=(const basic_string&lt;charT,traits,Allocator&gt;&amp; lhs,
                    const basic_string&lt;charT,traits,Allocator&gt;&amp; rhs) <ins>noexcept</ins>;
  [&hellip;]
  template&lt;class charT, class traits, class Allocator&gt;
    bool operator&gt;=(const basic_string&lt;charT,traits,Allocator&gt;&amp; lhs,
                    const basic_string&lt;charT,traits,Allocator&gt;&amp; rhs) <ins>noexcept</ins>;
  [&hellip;]
}
</pre></blockquote>
</li>

<li><p>Modify the class template <code>basic_string</code> synopsis in 27.4.3 <a href="https://wg21.link/basic.string">[basic.string]</a> as 
indicated (Remark 1: The <code>noexcept</code> at the move-constructor is fine, because even for a
small-object optimization there is no problem here, because <code>basic_string::value_type</code>
is required to be a non-array POD as of 27.1 <a href="https://wg21.link/strings.general">[strings.general]</a> p1, Remark 2: This
proposal removes the <code>noexcept</code> at single character overloads of <code>find</code>, <code>rfind</code>,
etc. because they are defined in terms of potentially allocating functions. It seems like
an additional issue to me to change the semantics in terms of non-allocating functions and
adding <code>noexcept</code> instead):</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 {
  public:
    [&hellip;]
    <i>// [string.ops], string operations:</i>
    [&hellip;]
    size_type find (charT c, size_type pos = 0) const <del>noexcept</del>;
    [&hellip;]
    size_type rfind(charT c, size_type pos = npos) const <del>noexcept</del>;
    [&hellip;]
    size_type find_first_of(charT c, size_type pos = 0) const <del>noexcept</del>;
    [&hellip;]
    size_type find_last_of (charT c, size_type pos = npos) const <del>noexcept</del>;
    [&hellip;]
    size_type find_first_not_of(charT c, size_type pos = 0) const <del>noexcept</del>;
    [&hellip;]
    size_type find_last_not_of (charT c, size_type pos = npos) const <del>noexcept</del>;
    [&hellip;]
  };
}
</pre></blockquote>

</li>

<li><p>Modify 27.4.3.8.2 <a href="https://wg21.link/string.find">[string.find]</a> before p5 and before p7 as indicated:</p>

<blockquote><pre>
size_type find(const charT* s, size_type pos = 0) const <del>noexcept</del>;
[&hellip;]
size_type find(charT c, size_type pos = 0) const <del>noexcept</del>;
</pre><blockquote><p>
-7- <i>Returns</i>: <code>find(basic_string&lt;charT,traits,Allocator&gt;(1,c), pos)</code>.
</p></blockquote></blockquote>

</li>

<li><p>Modify  [string.rfind] before p7 as indicated:</p>

<blockquote><pre>
size_type rfind(charT c, size_type pos = npos) const <del>noexcept</del>;
</pre><blockquote><p>
-7- <i>Returns</i>: <code>rfind(basic_string&lt;charT,traits,Allocator&gt;(1,c),pos)</code>.
</p></blockquote></blockquote>

</li>

<li><p>Modify  [string.find.first.of] before p7 as indicated:</p>

<blockquote><pre>
size_type find_first_of(charT c, size_type pos = 0) const <del>noexcept</del>;
</pre><blockquote><p>
-7- <i>Returns</i>: <code>find_first_of(basic_string&lt;charT,traits,Allocator&gt;(1,c), pos)</code>.
</p></blockquote></blockquote>

</li>

<li><p>Modify  [string.find.last.of] before p7 as indicated:</p>

<blockquote><pre>
size_type find_last_of(charT c, size_type pos = npos) const <del>noexcept</del>;
</pre><blockquote><p>
-7- <i>Returns</i>: <code>find_last_of(basic_string&lt;charT,traits,Allocator&gt;(1,c),pos)</code>.
</p></blockquote></blockquote>

</li>

<li><p>Modify  [string.find.first.not.of] before p7 as indicated:</p>

<blockquote><pre>
size_type find_first_not_of(charT c, size_type pos = 0) const <del>noexcept</del>;
</pre><blockquote><p>
-7- <i>Returns</i>: <code>find_first_not_of(basic_string(1, c), pos)</code>.
</p></blockquote></blockquote>

</li>

<li><p>Modify  [string.find.last.not.of] before p7 as indicated:</p>

<blockquote><pre>
size_type find_last_not_of(charT c, size_type pos = npos) const <del>noexcept</del>;
</pre><blockquote><p>
-7- <i>Returns</i>: <code>find_last_not_of(basic_string(1, c), pos)</code>.
</p></blockquote></blockquote>

</li>

<li><p>Modify  [string.operator==] before p2+p3 as indicated:</p>

<blockquote><pre>
template&lt;class charT, class traits, class Allocator&gt;
bool operator==(const charT* lhs,
                const basic_string&lt;charT,traits,Allocator&gt;&amp; rhs) <del>noexcept</del>;

[&hellip;]
				
template&lt;class charT, class traits, class Allocator&gt;
bool operator==(const basic_string&lt;charT,traits,Allocator&gt;&amp; lhs,
                const charT* rhs) <del>noexcept</del>;
</pre></blockquote>

</li>

<li><p>Modify  [string.op!=] before p2+p3 as indicated:</p>

<blockquote><pre>
template&lt;class charT, class traits, class Allocator&gt;
bool operator!=(const charT* lhs,
                const basic_string&lt;charT,traits,Allocator&gt;&amp; rhs) <del>noexcept</del>;

[&hellip;]
				
template&lt;class charT, class traits, class Allocator&gt;
bool operator!=(const basic_string&lt;charT,traits,Allocator&gt;&amp; lhs,
                const charT* rhs) <del>noexcept</del>;
</pre></blockquote>

</li>

<li><p>Modify  [string.op&lt;] before p2+p3 as indicated:</p>

<blockquote><pre>
template&lt;class charT, class traits, class Allocator&gt;
bool operator&lt;(const charT* lhs,
               const basic_string&lt;charT,traits,Allocator&gt;&amp; rhs) <del>noexcept</del>;

[&hellip;]
				
template&lt;class charT, class traits, class Allocator&gt;
bool operator&lt;(const basic_string&lt;charT,traits,Allocator&gt;&amp; lhs,
               const charT* rhs) <del>noexcept</del>;
</pre></blockquote>

</li>

<li><p>Modify  [string.op&gt;] before p2+p3 as indicated:</p>

<blockquote><pre>
template&lt;class charT, class traits, class Allocator&gt;
bool operator&gt;(const charT* lhs,
               const basic_string&lt;charT,traits,Allocator&gt;&amp; rhs) <del>noexcept</del>;

[&hellip;]
				
template&lt;class charT, class traits, class Allocator&gt;
bool operator&gt;(const basic_string&lt;charT,traits,Allocator&gt;&amp; lhs,
               const charT* rhs) <del>noexcept</del>;
</pre></blockquote>

</li>

<li><p>Modify  [string.op&lt;=] before p2+p3 as indicated:</p>

<blockquote><pre>
template&lt;class charT, class traits, class Allocator&gt;
bool operator&lt;=(const charT* lhs,
                const basic_string&lt;charT,traits,Allocator&gt;&amp; rhs) <del>noexcept</del>;

[&hellip;]
				
template&lt;class charT, class traits, class Allocator&gt;
bool operator&lt;=(const basic_string&lt;charT,traits,Allocator&gt;&amp; lhs,
                const charT* rhs) <del>noexcept</del>;
</pre></blockquote>

</li>

<li><p>Modify  [string.op&gt;=] before p2+p3 as indicated:</p>

<blockquote><pre>
template&lt;class charT, class traits, class Allocator&gt;
bool operator&gt;=(const charT* lhs,
                const basic_string&lt;charT,traits,Allocator&gt;&amp; rhs) <del>noexcept</del>;

[&hellip;]
				
template&lt;class charT, class traits, class Allocator&gt;
bool operator&gt;=(const basic_string&lt;charT,traits,Allocator&gt;&amp; lhs,
                const charT* rhs) <del>noexcept</del>;
</pre></blockquote>

</li>

<li><p>Modify 27.4.4.3 <a href="https://wg21.link/string.special">[string.special]</a> as indicated (Remark: The change of
the semantics guarantees as of 16.3.2.4 <a href="https://wg21.link/structure.specifications">[structure.specifications]</a> p4 that 
the "Throws: Nothing" element of member swap is implied):</p>

<blockquote><pre>
template&lt;class charT, class traits, class Allocator&gt;
  void swap(basic_string&lt;charT,traits,Allocator&gt;&amp; lhs,
    basic_string&lt;charT,traits,Allocator&gt;&amp; rhs) <del>noexcept</del>;
</pre><blockquote><p>
-1- Effects: <ins>Equivalent to</ins> <code>lhs.swap(rhs);</code>
</p></blockquote></blockquote>

</li>
</ol>





</body>
</html>
