<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 2436: Comparators for associative containers should always be CopyConstructible</title>
<meta property="og:title" content="Issue 2436: Comparators for associative containers should always be CopyConstructible">
<meta property="og:description" content="C++ library issue. Status: C++17">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue2436.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="2436"><a href="lwg-defects.html#2436">2436</a>. Comparators for associative containers should always be <code>CopyConstructible</code></h3>
<p><b>Section:</b> 23.2.7 <a href="https://wg21.link/associative.reqmts">[associative.reqmts]</a>, 23.2.8 <a href="https://wg21.link/unord.req">[unord.req]</a> <b>Status:</b> <a href="lwg-active.html#C++17">C++17</a>
 <b>Submitter:</b> Stephan T. Lavavej <b>Opened:</b> 2014-10-01 <b>Last modified:</b> 2018-06-23</p>
<p><b>Priority: </b>2
</p>
<p><b>View other</b> <a href="lwg-index-open.html#associative.reqmts">active issues</a> in [associative.reqmts].</p>
<p><b>View all other</b> <a href="lwg-index.html#associative.reqmts">issues</a> in [associative.reqmts].</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>
The associative container requirements attempt to permit comparators that are <code>DefaultConstructible</code> 
but non-<code>CopyConstructible</code>. However, the Standard contradicts itself. 23.4.3.1 <a href="https://wg21.link/map.overview">[map.overview]</a> depicts 
<code>map() : map(Compare()) { }</code> which requires both <code>DefaultConstructible</code> and <code>CopyConstructible</code>.
<p/>
Unlike fine-grained element requirements (which are burdensome for implementers, but valuable for users), such 
fine-grained comparator requirements are both burdensome for implementers (as the Standard's self-contradiction 
demonstrates) and worthless for users. We should unconditionally require <code>CopyConstructible</code> comparators.  
(Note that <code>DefaultConstructible</code> should remain optional; this is not problematic for implementers, and 
allows users to use lambdas.)
<p/>
Key equality predicates for unordered associative containers are also affected. However, 16.4.4.5 <a href="https://wg21.link/hash.requirements">[hash.requirements]</a>/1 
already requires hashers to be <code>CopyConstructible</code>, so 23.2.8 <a href="https://wg21.link/unord.req">[unord.req]</a>'s redundant wording should 
be removed.
</p>

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

<p>
GR: I prefer to say "<code>Compare</code>" rather than "<code>X::key_compare</code>", since the former is what the user supplies. 
JY: It makes sense to use "<code>Compare</code>" when we talk about requirements but "<code>key_compare</code>" when we use it. 
<p/>
AM: We're adding requirements here, which is a breaking change, even though nobody will ever have had a non-<code>CopyConstructible</code> 
comparator. But the simplification is probably worth it.
<p/>
GR: I don't care about unmovable containers. But I do worry that people might want to move they comparators. MC: How do 
you reconcile that with the function that says "give me the comparator"? GR: That one returns by value? JY: Yes. [To MC] 
You make it a requirement of that function. [To GR] And it [the <code>key_comp()</code> function] is missing its requirements. 
We need to add them everywhere. GR: map already has the right requirements.
<p/>
JM: I dispute this. If in C++98 a type wasn't copyable, it had some interesting internal state, but in C++98 you wouldn't 
have been able to pass it into the container since you would have had to make a copy. JY: No, you could have 
default-constructed it and never moved it, e.g. a mutex. AM: So, it's a design change, but one that we should make. 
That's probably an LEWG issue. AM: There's a contradiction in the Standard here, and we need to fix it one way or another.
<p/>
<b>Conclusion</b>: Move to LEWG 
</p>

<p><i>[LEWG: 2016-03, Jacksonville]</i></p>

<p>
Adding <code>CopyConstructible</code> requirement OK.
<p/>
Unanimous yes.
<p/>
We discussed allowing <code>MoveConstructible</code>. A moved-from <code>set&lt;&gt;</code> might still contain elements, 
and using them would become undefined if the comparator changed behavior.
</p>


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

<ol>
<li><p>Change 23.2.7 <a href="https://wg21.link/associative.reqmts">[associative.reqmts]</a> Table 102 as indicated (Editorial note: For "expression" <code>X::key_compare</code> 
"defaults to" is redundant with the class definitions for map/etc.):</p>

<blockquote>
<table border="1">
<caption>Table 102 &mdash; Associative container requirements</caption>
<tr>
<th>Expression</th>
<th>Return type</th>
<th>Assertion&#47;note pre-&#47;post-condition</th>
<th>Complexity</th>
</tr>

<tr>
<td colspan="4" align="center">
<code>&hellip;</code>
</td>
</tr>

<tr>
<td>
<code>X::key_compare</code>
</td>
<td>
<code>Compare</code>
</td>
<td>
<del>defaults to <code>less&lt;key_type&gt;</code></del><br/>
<ins><i>Requires</i>: <code>key_compare</code> is <code>CopyConstructible</code>.</ins>
</td>
<td>
compile time
</td>
</tr>

<tr>
<td colspan="4" align="center">
<code>&hellip;</code>
</td>
</tr>

<tr>
<td>
<code>X(c)<br/>
X a(c);</code>
</td>
<td>
</td>
<td>
<del><i>Requires</i>: <code>key_compare</code> is <code>CopyConstructible</code>.</del><br/>
<i>Effects</i>: Constructs an empty
container. Uses a copy of <code>c</code> as
a comparison object.
</td>
<td>
constant
</td>
</tr>

<tr>
<td colspan="4" align="center">
<code>&hellip;</code>
</td>
</tr>

<tr>
<td>
<code>X(i,j,c)<br/>
X a(i,j,c);</code>
</td>
<td>
</td>
<td>
<i>Requires</i>: <del><code>key_compare</code> is <code>CopyConstructible</code>.</del><br/>
<code>value_type</code> is <code>EmplaceConstructible</code> into <code>X</code> from <code>*i</code>.<br/>
<i>Effects</i>: Constructs [&hellip;]
</td>
<td>
[&hellip;]
</td>
</tr>

<tr>
<td colspan="4" align="center">
<code>&hellip;</code>
</td>
</tr>

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

<li><p>Change 23.2.8 <a href="https://wg21.link/unord.req">[unord.req]</a> Table 103 as indicated:</p>

<blockquote>
<table border="1">
<caption>Table 103 &mdash; Unordered associative container requirements (in addition to container)</caption>
<tr>
<th>Expression</th>
<th>Return type</th>
<th>Assertion&#47;note pre-&#47;post-condition</th>
<th>Complexity</th>
</tr>

<tr>
<td colspan="4" align="center">
<code>&hellip;</code>
</td>
</tr>

<tr>
<td>
<code>X::key_equal</code>
</td>
<td>
<code>Pred</code>
</td>
<td>
<ins><i>Requires</i>: <code>Pred</code> is <code>CopyConstructible</code>.</ins><br/>
<code>Pred</code> shall be a binary predicate that takes two arguments of type <code>Key</code>.<br/> 
<code>Pred</code> is an equivalence relation.
</td>
<td>
compile time
</td>
</tr>

<tr>
<td colspan="4" align="center">
<code>&hellip;</code>
</td>
</tr>

<tr>
<td>
<code>X(n, hf, eq)<br/>
X a(n, hf, eq);</code>
</td>
<td>
<code>X</code>
</td>
<td>
<del><i>Requires</i>: <code>hasher</code> and <code>key_equal</code> are <code>CopyConstructible</code>.</del><br/>
<i>Effects</i>: Constructs [&hellip;]
</td>
<td>
[&hellip;]
</td>
</tr>

<tr>
<td colspan="4" align="center">
<code>&hellip;</code>
</td>
</tr>

<tr>
<td>
<code>X(n, hf)<br/>
X a(n, hf);</code>
</td>
<td>
<code>X</code>
</td>
<td>
<i>Requires</i>: <del><code>hasher</code> is <code>CopyConstructible</code> and</del><br/>
<code>key_equal</code> is <code>DefaultConstructible</code><br/>
<i>Effects</i>: Constructs [&hellip;]
</td>
<td>
[&hellip;]
</td>
</tr>

<tr>
<td colspan="4" align="center">
<code>&hellip;</code>
</td>
</tr>

<tr>
<td>
<code>X(i, j, n, hf, eq)<br/>
X a(i, j, n, hf, eq);</code>
</td>
<td>
<code>X</code>
</td>
<td>
<i>Requires</i>: <del><code>hasher</code> and <code>key_equal</code> are <code>CopyConstructible</code>.</del><br/>
<code>value_type</code> is <code>EmplaceConstructible</code> into <code>X</code> from <code>*i</code>.<br/>
<i>Effects</i>: Constructs [&hellip;]
</td>
<td>
[&hellip;]
</td>
</tr>

<tr>
<td colspan="4" align="center">
<code>&hellip;</code>
</td>
</tr>

<tr>
<td>
<code>X(i, j, n, hf)<br/>
X a(i, j, n, hf);</code>
</td>
<td>
<code>X</code>
</td>
<td>
<i>Requires</i>: <del><code>hasher</code> is <code>CopyConstructible</code> and</del><br/>
<code>key_equal</code> is <code>DefaultConstructible</code><br/>
<code>value_type</code> is <code>EmplaceConstructible</code> into <code>X</code> from <code>*i</code>.<br/>
<i>Effects</i>: Constructs [&hellip;]
</td>
<td>
[&hellip;]
</td>
</tr>

<tr>
<td colspan="4" align="center">
<code>&hellip;</code>
</td>
</tr>

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






</body>
</html>
