<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 2274: Does map::operator[] value-initialize or default-insert a missing element?</title>
<meta property="og:title" content="Issue 2274: Does map::operator[] value-initialize or default-insert a missing element?">
<meta property="og:description" content="C++ library issue. Status: Resolved">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue2274.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="2274"><a href="lwg-defects.html#2274">2274</a>. Does <code>map::operator[]</code> value-initialize or default-insert a missing element?</h3>
<p><b>Section:</b> 23.4.3.3 <a href="https://wg21.link/map.access">[map.access]</a>, 23.5.3.3 <a href="https://wg21.link/unord.map.elem">[unord.map.elem]</a> <b>Status:</b> <a href="lwg-active.html#Resolved">Resolved</a>
 <b>Submitter:</b> Andrzej Krzemie&#324;ski <b>Opened:</b> 2013-07-16 <b>Last modified:</b> 2015-10-22</p>
<p><b>Priority: </b>3
</p>
<p><b>View all other</b> <a href="lwg-index.html#map.access">issues</a> in [map.access].</p>
<p><b>View all issues with</b> <a href="lwg-status.html#Resolved">Resolved</a> status.</p>
<p><b>Discussion:</b></p>
<p>
Suppose that I provide a custom allocator for type <code>int</code>, that renders value 1 rather than 0 in default-insertion:
</p>
<blockquote>
<pre>
struct Allocator1 : std::allocator&lt;int&gt;
{
  using super = std::allocator&lt;int&gt;;

  template&lt;typename Up, typename... Args&gt;
  void construct(Up* p, Args&amp;&amp;... args)
  { super::construct(p, std::forward&lt;Args&gt;(args)...); }

  template&lt;typename Up&gt;
  void construct(Up* p)
  { ::new((void*)p) Up(1); }
};
</pre>
</blockquote>

<p>
Now, if I use this allocator with <code>std::map</code>, and I use <code>operator[]</code> to access a not-yet-existent value, 
what value of the <code>mapped_type</code> should be created? 0 (value-initialization) or 1 (default-insertion):
</p>

<blockquote>
<pre>
map&lt;string, int, less&lt;string&gt;, Allocator1&gt; map;
cout &lt;&lt; map["cat"];
</pre>
</blockquote>

<p>
N3960 is not very clear. 23.4.3.3 <a href="https://wg21.link/map.access">[map.access]</a> in para 1 says:
</p>
<blockquote>
<p>
"If there is no key equivalent to <code>x</code> in the map, inserts <code>value_type(x, T())</code> into the map."
</p>
</blockquote>
<p>
So, it requires value-initialization.
<p/>
But para 2 says:
</p>
<blockquote>
<p>
"<code>mapped_type</code> shall be <code>DefaultInsertable</code> into <code>*this</code>."
</p>
</blockquote>
<p>
This implies default-insertion, because if not, why the requirement. Also similar functions like 
<code>vector::resize</code> already require default-insertion wherever they put <code>DefaultInsertable</code> requirements.
<p/>
Not to mention that default-insertion is more useful, because it allows custom allocators to "override" the 
default value of <code>mapped_type</code>.
</p>

<p><i>[2013-09 Chicago]</i></p>

<p>
Alisdair: Matters only for POD or trivial types 
<p/>
Marshall: issue might show up elsewhere other than <code>map&lt;&gt;</code> 
<p/>
Alisdair: initialize elements in any containers &mdash; by calling construct on allocator traits 
<p/>
Marshall: existing wording is clear 
<p/>
Alisdair: main concern is difference in wording, discusses default initialization 
<p/>
Nico: different requirement needed 
<p/>
Alisdair: gut is issue is NAD, brings up <code>DefaultInsertable</code> definition &mdash; discusses definition 
<p/>
Nico: why do we have the requirement? 
<p/>
Alisdair: other containers have this requirement 
<p/>
Marshall: this applies to many other containers 
<p/>
Nico: <code>deque&lt;&gt;</code> in particular 
<p/>
Alisdair: discusses allocator construct 
<p/>
Alisdair: wording raises concerns that aren't said in existing standard 
<p/>
Nico: sees no benefit to change 
<p/>
Marshall: leery of change 
<p/>
Alisdair: can be made clearer; might need to add note to <code>DefaultInsertable</code>; borderline editorial, 
comfortable without note, willing to wait until other issues arise. close issue as NAD
</p>

<p><i>[2015-01-20: Tomasz Kami&nacute;ski comments]</i></p>

<p>
With the addition of the <code>try_emplace</code> method the behavior of the <code>operator[]</code> for the maps, 
may be defined as follows:
</p>
<blockquote>
<pre>
T&amp; operator[](const key_type&amp; x);
</pre>
<blockquote>
<p>
<i>Effects</i>: Equivalent to: <code>try_emplace(x).first-&gt;second</code>;
</p>
</blockquote>
<pre>
T&amp; operator[](key_type&amp;&amp; x);
</pre>
<blockquote>
<p>
<i>Effects</i>: Equivalent to <code>try_emplace(std::move(x)).first-&gt;second</code>;
</p>
</blockquote>
</blockquote>
<p>
This would simplify the wording and also after resolution of the issue <a href="lwg-defects.html#2464" title="try_emplace and insert_or_assign misspecified (Status: C++17)">2464</a><sup><a href="https://cplusplus.github.io/LWG/issue2464" title="Latest snapshot">(i)</a></sup>, this wording would 
also address this issue.
</p>

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

<p>
Wait until <a href="lwg-defects.html#2464" title="try_emplace and insert_or_assign misspecified (Status: C++17)">2464</a><sup><a href="https://cplusplus.github.io/LWG/issue2464" title="Latest snapshot">(i)</a></sup> and <a href="lwg-defects.html#2469" title="Wrong specification of Requires clause of operator[] for map and unordered_map (Status: C++17)">2469</a><sup><a href="https://cplusplus.github.io/LWG/issue2469" title="Latest snapshot">(i)</a></sup> are in, which solve this.
</p>

<p><i>[2015-05-06 Lenexa: This is resolved by <a href="lwg-defects.html#2469" title="Wrong specification of Requires clause of operator[] for map and unordered_map (Status: C++17)">2469</a><sup><a href="https://cplusplus.github.io/LWG/issue2469" title="Latest snapshot">(i)</a></sup>.]</i></p>



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

<ol>
<li><p>Change 23.4.3.3 <a href="https://wg21.link/map.access">[map.access]</a> p1+p5 as indicated:</p>

<blockquote>
<pre>
T&amp; operator[](const key_type&amp; x);
</pre>
<blockquote>
<p>
-1- <i>Effects:</i> If there is no key equivalent to <code>x</code> in the map, inserts <del><code>value_type(x, T())</code> 
into the map</del><ins>into the map a value with <code>key_type</code> initialized using expression <code>x</code> and <code>mapped_type</code> 
initialized by default-insertion</ins>.
<p/>
-2- <i>Requires:</i> <code>key_type</code> shall be <code>CopyInsertable</code> and <code>mapped_type</code> shall be 
<code>DefaultInsertable</code> into <code>*this</code>.
<p/>
[&hellip;]
</p>
</blockquote>
<pre>
T&amp; operator[](key_type&amp;&amp; x);
</pre>
<blockquote>
<p>
-5- <i>Effects:</i> If there is no key equivalent to <code>x</code> in the map, inserts 
<del><code>value_type(std::move(x), T())</code> into the map</del><ins>into the map a value with <code>key_type</code> 
initialized using expression <code>std::move(x)</code> and <code>mapped_type</code> initialized by default-insertion</ins>.
<p/>
-6- <i>Requires:</i> <code>mapped_type</code> shall be <code>DefaultInsertable</code> into <code>*this</code>.
</p>
</blockquote>
</blockquote>
</li>

<li><p>Change 23.5.3.3 <a href="https://wg21.link/unord.map.elem">[unord.map.elem]</a> p2 as indicated:</p>

<blockquote>
<pre>
mapped_type&amp; operator[](const key_type&amp; k);
mapped_type&amp; operator[](key_type&amp;&amp; k);
</pre>
<blockquote>
<p>
-1- <i>Requires:</i> <code>mapped_type</code> shall be <code>DefaultInsertable</code> into <code>*this</code>. 
For the first operator, <code>key_type</code> shall be <code>CopyInsertable</code> into <code>*this</code>. For the second 
operator, <code>key_type</code> shall be <code>MoveConstructible</code>.
<p/>
-2- <i>Effects:</i> If the <code>unordered_map</code> does not already contain an element whose key is equivalent to 
<code>k</code>, the first operator inserts <del>the value <code>value_type(k, mapped_type())</code></del><ins>a value with 
<code>key_type</code> initialized using expression <code>x</code> and <code>mapped_type</code> initialized by default-insertion</ins> 
and the second operator inserts <del>the value <code>value_type(std::move(k), mapped_type())</code></del><ins>a value 
with <code>key_type</code> initialized using expression <code>std::move(x)</code> and <code>mapped_type</code> initialized by 
default-insertion</ins>.
</p>
</blockquote>
</blockquote>
</li>
</ol>






</body>
</html>
