<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 4048: Inconsistent preconditions for transparent insertion of std::flat_map/std::flat_set</title>
<meta property="og:title" content="Issue 4048: Inconsistent preconditions for transparent insertion of std::flat_map/std::flat_set">
<meta property="og:description" content="C++ library issue. Status: New">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue4048.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#New">New</a> status.</em></p>
<h3 id="4048"><a href="lwg-active.html#4048">4048</a>. Inconsistent preconditions for transparent insertion of <code>std::flat_map/std::flat_set</code></h3>
<p><b>Section:</b> 23.6.11 <a href="https://wg21.link/flat.set">[flat.set]</a>, 23.6.8.1 <a href="https://wg21.link/flat.map.overview">[flat.map.overview]</a> <b>Status:</b> <a href="lwg-active.html#New">New</a>
 <b>Submitter:</b> Jiang An <b>Opened:</b> 2024-01-26 <b>Last modified:</b> 2024-03-15</p>
<p><b>Priority: </b>2
</p>
<p><b>View all issues with</b> <a href="lwg-status.html#New">New</a> status.</p>
<p><b>Discussion:</b></p>
<p>
The preconditions for transparent insertion of associative containers (23.4.3.4 <a href="https://wg21.link/map.modifiers">[map.modifiers]</a>/13, 
23.4.3.4 <a href="https://wg21.link/map.modifiers">[map.modifiers]</a>/29, and 23.4.6.4 <a href="https://wg21.link/set.modifiers">[set.modifiers]</a>/3) detect the results of <code>equal_range</code>, 
while those for <code>std::flat_set</code> and <code>std::flat_map</code> (23.6.11.5 <a href="https://wg21.link/flat.set.modifiers">[flat.set.modifiers]</a>/2 and 
23.6.8.7 <a href="https://wg21.link/flat.map.modifiers">[flat.map.modifiers]</a>/20) currently detect the results of <code>find</code>, which is inconsistent.
<p/>
During implementing <code>std::flat_set</code> in MSVC STL, it was reported 
(<a href="https://github.com/microsoft/STL/issues/4105">microsoft/STL#4105</a>) that the current preconditions 
for <code>std::flat_set::insert</code> can lead to inconsistent results. Tim Song told that the current preconditions 
were copied from old revisions of <a href="https://wg21.link/P2363" title=" Extending associative containers with the remaining heterogeneous overloads">P2363</a>. So, presumably we should change these preconditions for 
flat container adaptors to consistently use <code>equal_range</code>.
</p>

<p><i>[2024-03-15; Reflector poll]</i></p>

<p>
Set priority to 2 after reflector poll.
</p>
<p>
We didn't want <code>equal_range(k) == equal_range(u)</code> because the two sides of the
<code>==</code> need to be evaluated at different times.
</p>
<p>
It seems to me that <code>equal_range(k) == equal_range(u)</code> ought to be
true before the insertion, and still true after the insertion.
The wording in 23.4.6.4 <a href="https://wg21.link/set.modifiers">[set.modifiers]</a>/3 and
23.4.3.4 <a href="https://wg21.link/map.modifiers">[map.modifiers]</a>/13 requires only that the condition be true
before the insertion.
We either want to change the wording here to be consistent with those places;
or else change 23.4.6.4 <a href="https://wg21.link/set.modifiers">[set.modifiers]</a> and 23.4.3.4 <a href="https://wg21.link/map.modifiers">[map.modifiers]</a>
to be consistent with the saner wording here. I'd vote for the latter.
</p>



<p id="res-4048"><b>Proposed resolution:</b></p>
<p>
This wording is relative to <a href="https://wg21.link/N4971" title=" Working Draft, Programming Languages — C++">N4971</a>.
</p>

<ol>
<li><p>Modify 23.6.8.7 <a href="https://wg21.link/flat.map.modifiers">[flat.map.modifiers]</a> as indicated:</p>

<blockquote>
<pre>
template&lt;class K, class... Args&gt;
  pair&lt;iterator, bool&gt; try_emplace(K&amp;&amp; k, Args&amp;&amp;... args);
template&lt;class K, class... Args&gt;
  iterator try_emplace(const_iterator hint, K&amp;&amp; k, Args&amp;&amp;... args);
</pre>
<blockquote>
<p>
[&hellip;]
<p/>
-20- <i>Preconditions</i>: The conversion from <code>k</code> into <code>key_type</code> constructs an 
object <code>u</code>, for which <code><del>find(k) == find(u)</del><ins>equal_range(k) == equal_range(u)</ins></code> 
is <code>true</code>.
</p>
</blockquote>
</blockquote>
</li>

<li><p>Modify 23.6.11.5 <a href="https://wg21.link/flat.set.modifiers">[flat.set.modifiers]</a> as indicated:</p>

<blockquote>
<pre>
template&lt;class K&gt; pair&lt;iterator, bool&gt; insert(K&amp;&amp; x);
template&lt;class K&gt; iterator insert(const_iterator hint, K&amp;&amp; x);
</pre>
<blockquote>
<p>
[&hellip;]
<p/>
-2- <i>Preconditions</i>: The conversion from <code>x</code> into <code>value_type</code> constructs an 
object <code>u</code>, for which <code><del>find(x) == find(u)</del><ins>equal_range(x) == equal_range(u)</ins></code> 
is <code>true</code>.
</p>
</blockquote>
</blockquote>
</li>
</ol>





</body>
</html>
