<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 3577: Merging an (unordered) associative container with itself</title>
<meta property="og:title" content="Issue 3577: Merging an (unordered) associative container with itself">
<meta property="og:description" content="C++ library issue. Status: New">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue3577.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="3577"><a href="lwg-active.html#3577">3577</a>. Merging an (unordered) associative container with itself</h3>
<p><b>Section:</b> 23.2.7.1 <a href="https://wg21.link/associative.reqmts.general">[associative.reqmts.general]</a>, 23.2.8.1 <a href="https://wg21.link/unord.req.general">[unord.req.general]</a> <b>Status:</b> <a href="lwg-active.html#New">New</a>
 <b>Submitter:</b> Joaqu&iacute;n M L&oacute;pez Mu&ntilde;oz <b>Opened:</b> 2021-08-04 <b>Last modified:</b> 2024-01-29</p>
<p><b>Priority: </b>3
</p>
<p><b>View other</b> <a href="lwg-index-open.html#associative.reqmts.general">active issues</a> in [associative.reqmts.general].</p>
<p><b>View all other</b> <a href="lwg-index.html#associative.reqmts.general">issues</a> in [associative.reqmts.general].</p>
<p><b>View all issues with</b> <a href="lwg-status.html#New">New</a> status.</p>
<p><b>Discussion:</b></p>
<p>
For the expression <code>a.merge(a2)</code>, it is not explicitly stated whether <code>a2</code> can be the 
same object as <code>a</code>. libstdc++-v3 and libc++ seemingly assume this is not allowed, as the following code
produces an infinite loop with both standard library implementations:
</p>
<blockquote><pre>
#include &lt;set&gt;

int main()
{
  std::multiset&lt;int&gt; c={0, 0};
  c.merge(c);
}
</pre></blockquote>
<p>
A strict reading of postconditions seems to ban the case where <code>a</code> and <code>a2</code> are the same:
</p>
<ul>
<li><p>23.2.7.1 <a href="https://wg21.link/associative.reqmts.general">[associative.reqmts.general]</a>: "Iterators referring to the transferred elements [&hellip;] 
now behave as iterators into <code>a</code>, not into <code>a2</code>": if <code>a</code> and <code>a2</code> are the same, 
a transferred iterator can't be both an iterator to <code>a</code> and not an iterator to <code>a2</code>.</p></li>
<li><p>23.2.8.1 <a href="https://wg21.link/unord.req.general">[unord.req.general]</a>: "Iterators referring to the transferred elements and all iterators 
referring to <code>a</code> will be invalidated, but iterators to elements remaining in <code>a2</code> will remain valid": 
if <code>a</code> and <code>a2</code> are the same, an iterator can't both be invalidated and remain valid.</p></li>
</ul>
<p>
Even if a provision is made that, when <code>a</code> and <code>a2</code> are the same, no elements are transferred by 
convention, 23.2.8.1 <a href="https://wg21.link/unord.req.general">[unord.req.general]</a> would still implicitly ban the case, as all iterators would be 
invalidated but the iterators to the remaining elements (again, all iterators) would remain valid, which is 
contradictory.
<p/>
For context, analogous operations for <code>std::list</code> take inconsistent approaches:
</p>
<ul>
<li><p><code>splice(const_iterator position, list&amp; x)</code> requires that source and destination be not the same.</p></li>
<li><p><code>splice(const_iterator position, list&amp; x, const_iterator i)</code> implicitly allows <code>addressof(x) == this</code>, 
as the case <code>position == i</code> is taken care of.</p></li>
<li><p><code>std::list::merge</code> explicitly allows the case <code>addressof(x) == this</code> (resulting in a no-op).</p></li>
</ul>

<p><i>[2021-08-20; Reflector poll]</i></p>

<p>
Set priority to 3 after reflector poll.

Tim Song commented:
"I think the current PR of LWG<a href="lwg-active.html#2414" title="Member function reentrancy should be implementation-defined (Status: Open)">2414</a><sup><a href="https://cplusplus.github.io/LWG/issue2414" title="Latest snapshot">(i)</a></sup> bans this code,
but we might want to have consistency with <code>list::merge</code> instead."
</p>



<p id="res-3577"><b>Proposed resolution:</b></p>





</body>
</html>
