<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 2063: Contradictory requirements for string move assignment</title>
<meta property="og:title" content="Issue 2063: Contradictory requirements for string move assignment">
<meta property="og:description" content="C++ library issue. Status: C++17">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue2063.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++17">C++17</a> status.</em></p>
<h3 id="2063"><a href="lwg-defects.html#2063">2063</a>. Contradictory requirements for string move assignment</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++17">C++17</a>
 <b>Submitter:</b> Howard Hinnant <b>Opened:</b> 2011-05-29 <b>Last modified:</b> 2017-07-30</p>
<p><b>Priority: </b>3
</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++17">C++17</a> status.</p>
<p><b>Discussion:</b></p>
<p>
27.4.3.2 <a href="https://wg21.link/string.require">[string.require]</a>&#47;p4 says that <code>basic_string</code> is an "allocator-aware" 
container and behaves as described in 23.2.2 <a href="https://wg21.link/container.requirements.general">[container.requirements.general]</a>.
<p/>
23.2.2 <a href="https://wg21.link/container.requirements.general">[container.requirements.general]</a> describes move assignment in p7 and Table 99.
<p/>
If <code>allocator_traits&lt;allocator_type&gt;::propagate_on_container_move_assignment::value</code> 
is false, and if the allocators stored in the lhs and rhs sides are not equal, then move 
assigning a string has the same semantics as copy assigning a string as far as resources are 
concerned (resources can not be transferred). And in this event, the lhs may have to acquire 
resources to gain sufficient capacity to store a copy of the rhs.
<p/>
However 27.4.3.3 <a href="https://wg21.link/string.cons">[string.cons]</a>&#47;p22 says:
</p><blockquote><pre>
basic_string&lt;charT,traits,Allocator&gt;&amp;
operator=(basic_string&lt;charT,traits,Allocator&gt;&amp;&amp; str) noexcept;
</pre><blockquote><p>
<i>Effects</i>: If <code>*this</code> and <code>str</code> are not the same object, modifies <code>*this</code> 
as shown in Table 71. [<i>Note</i>: A valid implementation is <code>swap(str)</code>. &mdash; <i>end note</i> ]
</p></blockquote></blockquote><p>
These two specifications for <code>basic_string::operator=(basic_string&amp;&amp;)</code> are in conflict with 
each other. It is not possible to implement a <code>basic_string</code> which satisfies both requirements.
<p/>
Additionally assign from an rvalue <code>basic_string</code> is defined as:
</p><blockquote><pre>
basic_string&amp; assign(basic_string&amp;&amp; str) noexcept;
</pre><blockquote><p>
<i>Effects</i>: The function replaces the string controlled by <code>*this</code> with a string of length 
<code>str.size()</code> whose elements are a copy of the string controlled by <code>str</code>. [ <i>Note</i>: A valid 
implementation is <code>swap(str)</code>. &mdash; <i>end note</i> ]
</p></blockquote></blockquote><p>
It seems contradictory that this member can be sensitive to <code>propagate_on_container_swap</code> instead 
of <code>propagate_on_container_move_assignment</code>.  Indeed, there is a very subtle chance for undefined 
behavior here:  If the implementation implements this in terms of <code>swap</code>, and if 
<code>propagate_on_container_swap</code> is false, and if the two allocators are unequal, the behavior 
is undefined, and will likely lead to memory corruption.  That's a lot to go wrong under a member 
named "assign".
</p>

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


<p>
Alisdair: Can this be conditional <code>noexcept</code>?
</p>
<p>
Pablo: We said we were not going to put in many conditional <code>noexcept</code>s. Problem is not allocator, but non-normative definition. It says swap is a valid operation which it is not.
</p>
<p>
Dave: Move assignment is not a critical method.
</p>
<p>
Alisdair: Was confusing assignment and construction.
</p>
<p>
Dave: Move construction is critical for efficiency.
</p>
<p>
Kyle: Is it possible to test for <code>noexcept</code>.
</p>
<p>
Alisdair: Yes, query the <code>noexcept</code> operator.
</p>
<p>
Alisdair: Agreed there is a problem that we cannot unconditionally mark these operations as <code>noexcept</code>.
</p>
<p>
Pablo: How come swap is not defined in alloc
</p>
<p>
Alisdair: It is in utility.
</p>
<p>
Pablo: Swap has a conditional <code>noexcept</code>. Is no throw move constructable, is no throw move assignable.
</p>
<p>
Pablo: Not critical for strings or containers.
</p>
<p>
Kyle: Why?
</p>
<p>
Pablo: They do not use the default swap.
</p>
<p>
Dave: Important for deduction in other types.
</p>
<p>
Alisdair: Would change the policy we adopted during FDIS mode.
</p>
<p>
Pablo: Keep it simple and get some vendor experience.
</p>
<p>
Alisdair: Is this wording correct? Concerned with bullet 2.
</p>
<p>
Pablo: Where does it reference containers section.
</p>
<p>
Alisdair: String is a container.
</p>
<p>
Alisdair: We should not remove redundancy piecemeal.
</p>
<p>
Pablo: I agree. This is a deviation from rest of string. Missing forward reference to containers section.
</p>
<p>
Pablo: To fix section 2. Only the note needs to be removed. The rest needs to be a forward reference to containers.
</p>
<p>
Alisdair: That is a new issue.
</p>
<p>
Pablo: Not really. Talking about adding one sentence, saying that basic string is a container.
</p>
<p>
Dave: That is not just a forward reference, it is a semantic change.
</p>
<p>
PJ: We intended to make it look like a container, but it did not satisfy all the requirements.
</p>
<p>
Pablo: Clause 1 is correct. Clause 2 is removing note and <code>noexcept</code> (do not remove the rest). Clause 3 is correct.
</p>
<p>
Alisdair: Not sure data() is correct (in clause 2).
</p>
<p>
Conclusion: Move to open, Alisdair and Pablo volunteered to provide wording
</p>

<p><i>[
originally proposed wording:
]</i></p>


<p>This wording is relative to the FDIS.</p>
<ol>
<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>:</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;]
    basic_string&amp; operator=(basic_string&amp;&amp; str) <del>noexcept</del>;
    [&hellip;]
    basic_string&amp; assign(basic_string&amp;&amp; str) <del>noexcept</del>;
    [&hellip;]
  };
}
</pre></blockquote>
</li>

<li><p>Remove the definition of the <code>basic_string</code> move assignment operator from 27.4.3.3 <a href="https://wg21.link/string.cons">[string.cons]</a> 
entirely, including Table 71 &mdash; <code>operator=(const basic_string&lt;charT, traits, Allocator&gt;&amp;&amp;)</code>.
This is consistent with how we define move assignment for the containers in Clause 23:</p>
<blockquote><pre>
<del>basic_string&lt;charT,traits,Allocator&gt;&amp;
operator=(basic_string&lt;charT,traits,Allocator&gt;&amp;&amp; str) noexcept;</del>
</pre><blockquote><p>
<del>-22- <i>Effects</i>: If <code>*this</code> and <code>str</code> are not the same object, modifies <code>*this</code> as shown 
in Table 71. [ <i>Note</i>: A valid implementation is <code>swap(str)</code>. &mdash; <i>end note</i> ]</del>
<p/>
<del>-23- If <code>*this</code> and <code>str</code> are the same object, the member has no effect.</del>
<p/>
<del>-24- <i>Returns</i>: <code>*this</code></del>
</p></blockquote></blockquote>
<blockquote>
<table border="1">
<caption><del>Table 71 &mdash; <code>operator=(const basic_string&lt;charT, traits, Allocator&gt;&amp;&amp;)</code></del></caption>

<tr>
<th><del>Element</del></th>
<th><del>Value</del></th>
</tr>

<tr>
<td><del><code>data()</code></del></td>
<td><del>points at the array whose first element was pointed
at by <code>str.data()</code></del></td>
</tr>

<tr>
<td><del><code>size()</code></del></td>
<td><del>previous value of <code>str.size()</code></del></td>
</tr>

<tr>
<td><del><code>capacity()</code></del></td>
<td><del>a value at least as large as <code>size()</code></del></td>
</tr>

</table> 
</blockquote>
</li>

<li><p>Modify the paragraphs prior to 27.4.3.7.3 <a href="https://wg21.link/string.assign">[string.assign]</a> p.3 as indicated (The
first insertion recommends a separate paragraph number for the indicated paragraph):</p>
<blockquote><pre>
basic_string&amp; assign(basic_string&amp;&amp; str) <del>noexcept</del>;
</pre><blockquote><p>
<ins>-?-</ins> <i>Effects</i>: <ins>Equivalent to <code>*this = std::move(str)</code>.</ins>
<del>The function replaces the string controlled by <code>*this</code> with a string of length 
<code>str.size()</code> whose elements are a copy of the string controlled by <code>str</code>. 
[ <i>Note</i>: A valid implementation is <code>swap(str)</code>. &mdash; <i>end note</i> ]</del>
<p/>
-3- <i>Returns</i>: <code>*this</code>
</p></blockquote></blockquote>

</li>
</ol>

<p><i>[
2012-08-11 Joe Gottman observes:
]</i></p>


<blockquote>
<p>
One of the effects of <code>basic_string</code>'s move-assignment operator (27.4.3.3 <a href="https://wg21.link/string.cons">[string.cons]</a>, Table 71) is
</p>
<blockquote>

<table border="1">

<tr>
<th>Element</th>
<th>Value</th>
</tr>

<tr>
<td><code>data()</code></td>
<td>points at the array whose first element was pointed at by <code>str.data()</code></td>
</tr>

</table> 

</blockquote>
<p>
If a string implementation uses the small-string optimization and the input string <code>str</code> is small enough 
to make use of it, this effect is impossible to achieve. To use the small string optimization, a string has to 
be implemented using something like
</p>
<blockquote><pre>
union
{
   char buffer[SMALL_STRING_SIZE];
   char *pdata;
};
</pre></blockquote>
<p>
When the string is small enough to fit inside <code>buffer</code>, the <code>data()</code> member function returns 
<code>static_cast&lt;const char *&gt;(buffer)</code>, and since <code>buffer</code> is an array variable, there 
is no way to implement move so that the moved-to string's <code>buffer</code> member variable is equal to 
<code>this->buffer</code>.
<p/>
Resolution proposal:
<p/>
Change Table 71 to read:
</p>
<blockquote>

<table border="1">

<tr>
<th>Element</th>
<th>Value</th>
</tr>

<tr>
<td><code>data()</code></td>
<td>points at the array <del>whose first element was pointed at by <code>str.data()</code></del>
<ins>that contains the same characters in the same order as <code>str.data()</code> contained before 
<code>operator=()</code> was called</ins></td>
</tr>

</table> 

</blockquote>
<blockquote>
</blockquote>
</blockquote>

<p><i>[2015-05-07, Lenexa]</i></p>

<p>
Howard suggests improved wording
<p/>
Move to Immediate
</p>



<p id="res-2063"><b>Proposed resolution:</b></p>
<p>This wording is relative to N4431.</p>

<ol>
<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>:</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;]
    basic_string&amp; assign(basic_string&amp;&amp; str) noexcept<ins>(
         allocator_traits&lt;Allocator&gt;::propagate_on_container_move_assignment::value ||
           allocator_traits&lt;Allocator&gt;::is_always_equal::value)</ins>;
    [&hellip;]
  };
}
</pre></blockquote>
</li>

<li><p>Change 27.4.3.3 <a href="https://wg21.link/string.cons">[string.cons]</a>/p21-23:</p>
<blockquote><pre>
basic_string&amp;
operator=(basic_string&amp;&amp; str) noexcept(
         allocator_traits&lt;Allocator&gt;::propagate_on_container_move_assignment::value ||
           allocator_traits&lt;Allocator&gt;::is_always_equal::value);
</pre><blockquote><p>
-21- <i>Effects</i>: <del>If <code>*this</code> and <code>str</code> are not the same object, modifies <code>*this</code> as shown
in Table 71. [ <i>Note</i>: A valid implementation is <code>swap(str)</code>. &mdash; <i>end note</i> ]</del>
<ins>Move assigns as a sequence container ([container.requirements]), except that iterators, pointers and references may be invalidated.</ins>
<p/>
<del>-22- If <code>*this</code> and <code>str</code> are the same object, the member has no effect.</del>
<p/>
-23- <i>Returns</i>: <code>*this</code>
</p></blockquote></blockquote>
<blockquote>
<table border="1">
<caption><del>Table 71 &mdash; <code>operator=(basic_string&amp;&amp;)</code> effects</del></caption>

<tr>
<th><del>Element</del></th>
<th><del>Value</del></th>
</tr>

<tr>
<td><del><code>data()</code></del></td>
<td><del>points at the array whose first element was pointed
at by <code>str.data()</code></del></td>
</tr>

<tr>
<td><del><code>size()</code></del></td>
<td><del>previous value of <code>str.size()</code></del></td>
</tr>

<tr>
<td><del><code>capacity()</code></del></td>
<td><del>a value at least as large as <code>size()</code></del></td>
</tr>

</table>
</blockquote>
</li>

<li><p>Modify the paragraphs prior to 27.4.3.7.3 <a href="https://wg21.link/string.assign">[string.assign]</a> p.3 as indicated</p>
<blockquote><pre>
basic_string&amp; assign(basic_string&amp;&amp; str) noexcept<ins>(
         allocator_traits&lt;Allocator&gt;::propagate_on_container_move_assignment::value ||
           allocator_traits&lt;Allocator&gt;::is_always_equal::value)</ins>;
</pre><blockquote><p>
-3- <i>Effects</i>: <ins>Equivalent to <code>*this = std::move(str)</code>.</ins>
<del>The function replaces the string controlled by <code>*this</code> with a string of length
<code>str.size()</code> whose elements are a copy of the string controlled by <code>str</code>.
[ <i>Note</i>: A valid implementation is <code>swap(str)</code>. &mdash; <i>end note</i> ]</del>
<p/>
-4- <i>Returns</i>: <code>*this</code>
</p></blockquote></blockquote>

</li>
</ol>





</body>
</html>
