<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 2076: Bad CopyConstructible requirement in set constructors</title>
<meta property="og:title" content="Issue 2076: Bad CopyConstructible requirement in set constructors">
<meta property="og:description" content="C++ library issue. Status: C++17">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue2076.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="2076"><a href="lwg-defects.html#2076">2076</a>. Bad <code>CopyConstructible</code> requirement in set constructors</h3>
<p><b>Section:</b> 23.4.6.2 <a href="https://wg21.link/set.cons">[set.cons]</a> <b>Status:</b> <a href="lwg-active.html#C++17">C++17</a>
 <b>Submitter:</b> Jens Maurer <b>Opened:</b> 2011-08-20 <b>Last modified:</b> 2017-07-30</p>
<p><b>Priority: </b>3
</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>
23.4.6.2 <a href="https://wg21.link/set.cons">[set.cons]</a> paragraph 4 says: 
</p>

<blockquote><p>
<i>Requires</i>: If the iterator's dereference operator returns an lvalue or a non-const rvalue, 
then <code>Key</code> shall be <code>CopyConstructible</code>.
</p></blockquote>

<p>
I'm confused why a "non-const rvalue" for the return value of the iterator
would require <code>CopyConstructible</code>; isn't that exactly the situation 
when you'd want to apply the move constructor?
<p/>
The corresponding requirement for <code>multimap</code> seems better in that regard
([multimap.cons] paragraph 3):
</p>
<blockquote><p>
Requires: If the iterator's dereference operator returns an lvalue or a const rvalue 
<code>pair&lt;key_type, mapped_type&gt;</code>, then both <code>key_type</code> and mapped_type 
shall be <code>CopyConstructible</code>.
</p></blockquote>
<p>
Obviously, if I have a const rvalue, I can't apply the move constructor (which will 
likely attempt modify its argument).
<p/>
Dave Abrahams:
<p/>
I think you are right.
Proposed resolution: drop "non-" from 23.4.6.2 <a href="https://wg21.link/set.cons">[set.cons]</a> paragraph 3.
</p>

<p><i>[2012, Kona]</i></p>

<p>
The wording is in this area will be affected by Pablo's paper being adopted at this meeting.
Wait for that paper to be applied before visiting this issue - deliberately leave in New
status until the next meeting.
</p>

<p><strong>Proposed resolution from Kona 2012:</strong></p>
<blockquote class="note">
<p>This wording is relative to the FDIS.</p>

<p>
Change 23.4.6.2 <a href="https://wg21.link/set.cons">[set.cons]</a> p3 as follows:
</p> 
<blockquote><pre>
template &lt;class InputIterator&gt;
  set(InputIterator first, InputIterator last,
    const Compare&amp; comp = Compare(), const Allocator&amp; = Allocator());
</pre><blockquote><p>
-3- Effects: Constructs an empty set using the specified comparison object and allocator, and inserts
elements from the range [<code>first,last</code>).
<p/>
-4- <i>Requires</i>: If the iterator's dereference operator returns an lvalue or a <del>non-</del>const rvalue, 
then <code>Key</code> shall be <code>CopyConstructible</code>.
<p/>
-5- <i>Complexity</i>: Linear in <code>N</code> if the range [<code>first,last</code>) is already sorted using 
<code>comp</code> and otherwise <code>N logN</code>, where <code>N</code> is <code>last - first</code>.
</p></blockquote></blockquote>
</blockquote>

<p><i>[2014-05-18, Daniel comments]</i></p>

<p>
According to Pablo, the current P/R correctly incorporates the changes from his paper (which was adopted in Kona)
</p>

<p><i>[2014-06-10, STL comments and suggests better wording]</i></p>

<p>
<a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2005/n1858.html">N1858</a> was voted into WP 
<a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2284.pdf">N2284</a> but was 
<a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2283.html">"(reworded)"</a>, introducing the "non-const" damage.
<p/>
N1858 wanted to add this for map:
</p>
<blockquote><p>
<i>Requires</i>: Does not require <code>CopyConstructible</code> of either <code>key_type</code> or <code>mapped_type</code> if the dereferenced 
<code>InputIterator</code> returns a non-const rvalue <code>pair&lt;key_type, mapped_type&gt;</code>. Otherwise <code>CopyConstructible</code> 
is required for both <code>key_type</code> and <code>mapped_type</code>.
</p></blockquote>
<p>
And this for set:
</p>
<blockquote><p>
<i>Requires</i>: <code>Key</code> must be <code>CopyConstructible</code> only if the dereferenced <code>InputIterator</code> returns an lvalue 
or const rvalue.
</p></blockquote>
<p>
(And similarly for multi.)
<p/>
This was reworded to N2284 23.3.1.1 [map.cons]/3 and N2284 23.3.3.1 [set.cons]/4, and it slightly changed over the years into 
N3936 23.4.4.2 [map.cons]/3 and N3936 23.4.6.2 [set.cons]/4.
<p/>
In 2005/2007, this was the best known way to say "hey, we should try to move this stuff", as the fine-grained element requirements 
were taking shape.
<p/>
Then in 2010, <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2010/n3173.pdf">N3173</a> was 
<a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2010/n3226.html">voted</a> into WP 
<a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2010/n3225.pdf">N3225</a>, adding the definition of 
<code>EmplaceConstructible</code> and modifying the container requirements tables to make the range constructors require 
<code>EmplaceConstructible</code>.
<p/>
After looking at this history and double-checking our implementation (where <code>map</code>/<code>set</code> range construction goes through 
emplacement, with absolutely no special-casing for <code>map</code>'s pairs), I am convinced that N3173 superseded N1858 here. 
(Range-<code>insert()</code> and the unordered containers are unaffected.)
</p>

<p>
<strong>Previous resolution [SUPERSEDED]:</strong>
</p>
<blockquote class="note">
<p>This wording is relative to the N3936.</p>

<p>
Change 23.4.6.2 <a href="https://wg21.link/set.cons">[set.cons]</a> p4 as follows:
</p> 
<blockquote><pre>
template &lt;class InputIterator&gt;
  set(InputIterator first, InputIterator last,
    const Compare&amp; comp = Compare(), const Allocator&amp; = Allocator());
</pre><blockquote><p>
-3- Effects: Constructs an empty <code>set</code> using the specified comparison object and allocator, and inserts
elements from the range [<code>first,last</code>).
<p/>
-4- <i>Requires</i>: If the iterator's indirection operator returns an lvalue or a <del>non-</del>const rvalue, 
then <code>Key</code> shall be <code>CopyInsertible</code> into <code>*this</code>.
<p/>
-5- <i>Complexity</i>: Linear in <code>N</code> if the range [<code>first,last</code>) is already sorted using 
<code>comp</code> and otherwise <code>N logN</code>, where <code>N</code> is <code>last - first</code>.
</p></blockquote></blockquote>
</blockquote>

<p><i>[2015-02 Cologne]</i></p>

<p>
GR: Do requirements supersede rather than compose [container requirements and per-function requirements]? AM: Yes, they supersede.
<p/>
AM: This looks good. Ready? Agreement. 
</p>


<p id="res-2076"><b>Proposed resolution:</b></p>
<p>This wording is relative to the N4296.</p>

<ol>
<li><p>
Remove 23.4.3.2 <a href="https://wg21.link/map.cons">[map.cons]</a> p3:
</p> 
<blockquote><pre>
template &lt;class InputIterator&gt;
  map(InputIterator first, InputIterator last,
      const Compare&amp; comp = Compare(), const Allocator&amp; = Allocator());
</pre><blockquote>
<p>
<del>-3- <i>Requires</i>: If the iterator's indirection operator returns an lvalue or a <code>const</code> rvalue
<code>pair&lt;key_type, mapped_type&gt;</code>, then both <code>key_type</code> and <code>mapped_type</code> shall be 
<code>CopyInsertible</code> into <code>*this</code>.</del>
<p/>
[&hellip;]
</p></blockquote></blockquote>
</li>

<li><p>
Remove 23.4.4.2 <a href="https://wg21.link/multimap.cons">[multimap.cons]</a> p3:
</p> 
<blockquote><pre>
template &lt;class InputIterator&gt;
  multimap(InputIterator first, InputIterator last,
           const Compare&amp; comp = Compare(), 
           const Allocator&amp; = Allocator());
</pre><blockquote>
<p>
<del>-3- <i>Requires</i>: If the iterator's indirection operator returns an lvalue or a <code>const</code> rvalue
<code>pair&lt;key_type, mapped_type&gt;</code>, then both <code>key_type</code> and <code>mapped_type</code> shall be 
<code>CopyInsertible</code> into <code>*this</code>.</del>
<p/>
[&hellip;]
</p>
</blockquote></blockquote>
</li>

<li><p>
Remove 23.4.6.2 <a href="https://wg21.link/set.cons">[set.cons]</a> p4:
</p> 
<blockquote><pre>
template &lt;class InputIterator&gt;
  set(InputIterator first, InputIterator last,
      const Compare&amp; comp = Compare(), const Allocator&amp; = Allocator());
</pre><blockquote>
<p>
[&hellip;]
<p/>
<del>-4- <i>Requires</i>: If the iterator's indirection operator returns an lvalue or a non-<code>const</code> rvalue, then 
<code>Key</code> shall be <code>CopyInsertible</code> into <code>*this</code>.</del>
</p></blockquote></blockquote>
</li>

<li><p>
Remove 23.4.7.2 <a href="https://wg21.link/multiset.cons">[multiset.cons]</a> p3:
</p> 
<blockquote><pre>
template &lt;class InputIterator&gt;
  multiset(InputIterator first, InputIterator last,
           const Compare&amp; comp = Compare(), 
           const Allocator&amp; = Allocator());
</pre><blockquote>
<p>
<del>-3- <i>Requires</i>: If the iterator's indirection operator returns an lvalue or a <code>const</code> rvalue, then 
<code>Key</code> shall be <code>CopyInsertible</code> into <code>*this</code>.</del>
<p/>
[&hellip;]
</p></blockquote></blockquote>
</li>
</ol>





</body>
</html>
