<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 2397: map&lt;K, V&gt;::emplace and explicit V constructors</title>
<meta property="og:title" content="Issue 2397: map&lt;K, V&gt;::emplace and explicit V constructors">
<meta property="og:description" content="C++ library issue. Status: Resolved">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue2397.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#Resolved">Resolved</a> status.</em></p>
<h3 id="2397"><a href="lwg-defects.html#2397">2397</a>. <code>map&lt;K, V&gt;::emplace</code> and explicit <code>V</code> constructors</h3>
<p><b>Section:</b> 21.3.9.7 <a href="https://wg21.link/meta.trans.other">[meta.trans.other]</a> <b>Status:</b> <a href="lwg-active.html#Resolved">Resolved</a>
 <b>Submitter:</b> Peter Dimov <b>Opened:</b> 2014-06-12 <b>Last modified:</b> 2015-05-05</p>
<p><b>Priority: </b>1
</p>
<p><b>View all other</b> <a href="lwg-index.html#meta.trans.other">issues</a> in [meta.trans.other].</p>
<p><b>View all issues with</b> <a href="lwg-status.html#Resolved">Resolved</a> status.</p>
<p><b>Discussion:</b></p>
<p>
Please consider the following example:
</p>
<blockquote><pre>
#include &lt;map&gt;
#include &lt;atomic&gt;

int main()
{
   std::map&lt;int, std::atomic&lt;int&gt;&gt; map_;

   map_.emplace(1, 0);  // <span style="color:#C80000;font-weight:bold">fail</span>
   map_.emplace(1);     // <span style="color:#C80000;font-weight:bold">fail</span>
   map_.emplace(1, {}); // <span style="color:#C80000;font-weight:bold">fail</span>

   map_.emplace(std::piecewise_construct,
       std::tuple&lt;int&gt;(1), std::tuple&lt;>()); // <span style="color:#006600;font-weight:bold">OK</span>
}
</pre></blockquote>
<p>
The first three calls represent attempts by an ordinary programmer (in which role I appear today) to construct 
a map element. Since <code>std::atomic&lt;int&gt;</code> is non-copyable and immovable, I was naturally drawn to 
<code>emplace()</code> because it constructs in-place and hence doesn't need to copy or move. The logic behind the 
attempts was that <code>K=int</code> would be constructed from '<code>1</code>', and <code>V=std::atomic&lt;int&gt;</code> would be 
(directly) constructed by '<code>0</code>', default constructed, or constructed by '<code>{}</code>'.
<p/>
Yet none of the obvious attempts worked.
<p/>
I submit that at least two of the three ought to have worked, and that we have therefore a defect in either 
<code>map::emplace</code> or <code>pair</code>.
<p/>
Ville:
<p/>
There exists a related <a href="http://cplusplus.github.io/EWG/ewg-toc.html#114">EWG issue</a> for this.
<p/>
Daniel:
<p/>
If the proposal N4387 would be accepted, it would solve the first problem mentioned above.
</p>

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

<p>
AM: I think Peter's expectation is misguided that the second and third "//fail" cases should work.<br/> 
DK: Howard's paper [note: which hasn't been written yet] will make the second case work... AM: ...but 
the third one will never work without core changes.
<p/>
Case 1 is solved by DK's paper, cases 2 and 3 are not defects; at best they are extensions. 
</p>

<p><i>[2015-05, Lenexa]</i></p>

<p>
STL: think this is covered with N4387<br/>
MC: this was accepted in Cologne<br/>
STL: only want to fix the first emplace<br/>
MC: leave alone and mark as closed by N4387<br/>
</p>


<p id="res-2397"><b>Proposed resolution:</b></p>
<p>
Resolved by acceptance of <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/n4387.html">N4387</a>.
</p>





</body>
</html>
