<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 1302: different emplace semantics for sequence and associated containers</title>
<meta property="og:title" content="Issue 1302: different emplace semantics for sequence and associated containers">
<meta property="og:description" content="C++ library issue. Status: NAD">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue1302.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#NAD">NAD</a> status.</em></p>
<h3 id="1302"><a href="lwg-closed.html#1302">1302</a>. different <code>emplace</code> semantics for sequence and associated containers</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#NAD">NAD</a>
 <b>Submitter:</b> Nicolai Josuttis <b>Opened:</b> 2010-01-03 <b>Last modified:</b> 2016-01-28</p>
<p><b>Priority: </b>Not Prioritized
</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#NAD">NAD</a> status.</p>
<p><b>Discussion:</b></p>
<p>
According to the new naming scheme introduced with
<a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2680.pdf">N2680</a>
</p>

<blockquote><pre>
vector&lt;T&gt; v;
v.emplace(v.begin(),x,y,z)
</pre></blockquote>

<p>
now has a different semantics than
</p>

<blockquote><pre>
set&lt;T&gt; s;
s.emplace(s.begin(),x,y,z);
</pre></blockquote>

<p>
While the version for <code>vector</code>s takes the first argument as position and
the remaining for construction, the version for <code>set</code>s takes all
arguments for construction.
</p>

<p>
IMO, this is a serious design mistake for a couple of reasons:
</p>

<ul>
<li>
<p>
First, in principle, all STL member functions should have the same behavior with
the same member function to avoid confusion and allow to write proper generic
code.
</p>
<p>
In fact, when I write the following simple function template:
</p>
<blockquote><pre>
template &lt;typename T&gt;
void doEmplace (T&amp; cont)
{
   cont.emplace(cont.begin(),"nico","josuttis",42);
}
</pre></blockquote>
<p>
the semantics depends on the type of the container.
</p>
</li>
<li>
<p>
In addition, I also guess using the name <code>emplace_hint()</code> instead of
<code>emplace()</code> for associative containers is a design mistake. According to
my knowledge, it was a design goal of the original STL to provide ONE
<code>insert</code> function, which works for ALL containers. This was
<code>insert(pos,val)</code>.
</p>
<p>
The trick to declare <code>pos</code> as a hint, allowed that we could implement a
generic <code>insert</code> for all containers. Now, with the new <code>emplace</code>
naming scheme, this trick is gone for the new kind of insertion.
</p>
</li>
</ul>

<p>
I consider this to be a serious design penalty because once this
is specified we can't fix that without breaking backward compatibility.
</p>

<p>
However, we have two choices for a fix:
</p>

<ul>
<li>
rename <code>emplace_hint(pos,val)</code> for associative containers back to
<code>emplace(pos,val)</code>. However to avoid the overloading problems, we also
have to rename the existing <code>emplace(val)</code> functions to something else (I
don't have a good name here at hand).
</li>
<li>
Keep <code>emplace(val)</code> for associative containers as it is, but rename
<code>emplace(pos,val)</code> for sequence containers and
<code>emplace_hint(pos,val)</code> to something like <code>emplace_at(pos,val)</code>,
declaring that <code>pos</code> is a hint for associative containers.
</li>
</ul>

<p><i>[
2010 Pittsburgh:  Moved to NAD, rationale added below.
]</i></p>




<p><b>Rationale:</b></p>
<p>
There was no consensus to make this change.
</p>


<p id="res-1302"><b>Proposed resolution:</b></p>
<p> In 23.2.8 <a href="https://wg21.link/unord.req">[unord.req]</a>, change: </p>
<blockquote> 
  <table border="1">
    <caption>Table 96 &mdash; Associative container requirements (in addition to 
    container)</caption>
    <tr> 
      <th>expression</th>
      <th>Return type</th>
      <th>Assertion/note pre-/post-condition</th>
      <th>Post-condition</th>
    </tr>
    <tr> 
      <td colspan="4">...</td>
    </tr>
    <tr> 
      <td><code>a_uniq.emplace<ins>_value</ins>(args)</code></td>
      <td><code>pair&lt;iterator, bool&gt;</code></td>
      <td>inserts a T object t constructed with std::forward&lt;Args&gt;(args)...<br/>
        if and only if there is no element in the container with key equivalent 
        to the key of t.<br/>
        The bool component of the returned pair is true if and only if the insertion 
        takes place, and the iterator component of the pair points to the element 
        with key equivalent to the key of t.</td>
      <td>logarithmic</td>
    </tr>
    <tr> 
      <td><code>a_eq.emplace<ins>_value</ins>(args)</code></td>
      <td><code>iterator</code></td>
      <td>inserts a T object t constructed with std::forward&lt;Args&gt;(args)... 
        and returns the iterator pointing to the newly inserted element.</td>
      <td>logarithmic</td>
    </tr>
    <tr> 
      <td><code>a.emplace<del>_hint</del>(p,args)</code></td>
      <td><code>iterator</code></td>
      <td>equivalent to
      <code>a.emplace<ins>_value</ins>(std::forward&lt;Args&gt;(args)...)</code>.
      Return value is an iterator pointing to the element with the key
      equivalent to the newly inserted element. The const_iterator p is a hint
      pointing to where the search should start. Implementations are permitted
      to ignore the hint.</td> <td>logarithmic in general, but amortized
      constant if the element is inserted right after p</td>
    </tr>
    <tr> 
      <td colspan="4">... </td>
    </tr>
  </table>
  
</blockquote>
<p> In 23.2.8 <a href="https://wg21.link/unord.req">[unord.req]</a>, change: </p>
<blockquote>
  <table border="1">
    <caption>Table 98 &mdash; Unordered associative container requirements (in 
    addition to container)</caption>
    <tr> 
      <th>expression</th>
      <th>Return type</th>
      <th>Assertion/note pre-/post-condition</th>
      <th>Post-condition</th>
    </tr>
    <tr> 
      <td colspan="4">...</td>
    </tr>
    <tr> 
      <td><code>a_uniq.emplace<ins>_value</ins>(args)</code></td>
      <td><code>pair&lt;iterator, bool&gt;</code></td>
      <td>inserts a <code>T</code> object <code>t</code> constructed with <code>std::forward&lt;Args&gt;(args)...</code> if 
        and only if there is no element in the container with key equivalent to 
        the key of <code>t</code>. The bool component of the returned pair is true if and only 
        if the insertion takes place, and the iterator component of the pair points 
        to the element with key equivalent to the key of t.</td>
      <td>Average case O(1), worst case O(a_uniq.size()).</td>
    </tr>
    <tr> 
      <td><code>a_eq.emplace<ins>_value</ins>(args)</code></td>
      <td><code>iterator</code></td>
      <td>inserts a T object t constructed with std::forward&lt;Args&gt;(args)... 
        and returns the iterator pointing to the newly inserted element.</td>
      <td>Average case O(1), worst case O(a_eq.size()).</td>
    </tr>
    <tr> 
      <td><code>a.emplace<del>_hint</del>(p,args)</code></td>
      <td><code>iterator</code></td>
      <td>equivalent to
      <code>a.emplace<ins>_value</ins>(std::forward&lt;Args&gt;(args)...)</code>.
      Return value is an iterator pointing to the element with the key
      equivalent to the newly inserted element. The const_iterator p is a hint
      pointing to where the search should start. Implementations are permitted
      to ignore the hint.</td> <td>Average case O(1), worst case
      O(a.size()).</td>
    </tr>
    <tr> 
      <td colspan="4">... </td>
    </tr>
  </table>
</blockquote>

<p>
In 23.4.3 <a href="https://wg21.link/map">[map]</a>, 23.4.6 <a href="https://wg21.link/set">[set]</a>, 23.5.3 <a href="https://wg21.link/unord.map">[unord.map]</a>, 23.5.6 <a href="https://wg21.link/unord.set">[unord.set]</a>, change:
</p>
<blockquote> 
  <p><i>// modifiers:</i><br/>
    <code>template &lt;class... Args&gt; pair&lt;iterator, bool&gt; emplace<ins>_value</ins>(Args&amp;&amp;... 
    args);<br/>
    template &lt;class... Args&gt; iterator emplace<del>_hint</del>(const_iterator 
    position, Args&amp;&amp;... args);</code></p>
</blockquote>

<p>
In 23.4.4 <a href="https://wg21.link/multimap">[multimap]</a>, 23.4.7 <a href="https://wg21.link/multiset">[multiset]</a>, 23.5.4 <a href="https://wg21.link/unord.multimap">[unord.multimap]</a>, 23.5.7 <a href="https://wg21.link/unord.multiset">[unord.multiset]</a>, change:
</p>
<blockquote> 
  <p><i>// modifiers:<br/></i><code>template &lt;class... Args&gt; iterator emplace<ins>_value</ins>(Args&amp;&amp;... 
    args);<br/>
    template &lt;class... Args&gt; iterator emplace<del>_hint</del>(const_iterator position, 
    Args&amp;&amp;... args);<br/>
    </code> </p>
</blockquote>





</body>
</html>
