{"id":108916,"date":"2023-10-23T07:00:00","date_gmt":"2023-10-23T14:00:00","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/oldnewthing\/?p=108916"},"modified":"2023-10-23T06:30:53","modified_gmt":"2023-10-23T13:30:53","slug":"20231023-00","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20231023-00\/?p=108916","title":{"rendered":"How do I add a non-copyable, non-movable object to a <CODE>std::map<\/CODE> or <CODE>std::unordered_map<\/CODE>?"},"content":{"rendered":"<p>Suppose you have a C++ class that is non-copyable and non-movable. This can happen if it has a member which is non-copyable and non-movable, like a <code>std::mutex<\/code>. But how do you put an object of this class into a map?<\/p>\n<pre>struct weird\r\n{\r\n    int value;\r\n    std::mutex mtx;\r\n};\r\n\r\nstd::map&lt;int, weird&gt; table;\r\n\r\ntable.insert({ 1, {} }); \/\/ nope\r\ntable.insert_or_assign(1, weird{}); \/\/ nope\r\ntable.emplace({ 1, {} }); \/\/ nope\r\ntable.emplace(1, weird{}); \/\/ nope\r\ntable.try_emplace(1, weird{}); \/\/ nope\r\n<\/pre>\n<p>The problem with the <code>insert<\/code> method is that it takes a <code>std::pair&lt;int, weird&gt;<\/code> by value, which means that it cannot move the <code>weird<\/code> to its final destination.<\/p>\n<p>The <code>insert_or_assign<\/code>, and <code>emplace<\/code> methods use the parameters to construct a <code>std::pair&lt;int, weird&gt;<\/code> at the final location, but since we passed a <code>weird{}<\/code> object as the second parameter, that would require moving the parameter into its final location.<\/p>\n<p>The <code>try_emplace<\/code> method uses its first parameter as a key and the rest of the parameters to construct the <code>weird<\/code>, so if you pass a brand new <code>weird<\/code>, it&#8217;s going to use the rvalue or lvalue constructor (as appropriate) to create the <code>weird<\/code> at its final location.<\/p>\n<p>Similar problems exist with <code>unordered_map<\/code>.<\/p>\n<p>One solution is to use <code>std::<wbr \/>piecewise_<wbr \/>construct<\/code>, (which we learned about <a title=\"What's up with std::piecewise_construct and std::forward_as_tuple?\" href=\"https:\/\/devblogs.microsoft.com\/oldnewthing\/20220428-00\/?p=106540\"> some time ago<\/a>), so that you can specify constructor parameters for each of the halves of the <code>std::<wbr \/>pair<\/code>:<\/p>\n<pre>table.emplace(std::piecewise_construct,\r\n    std::forward_as_tuple(1),\r\n    std::forward_as_tuple());\r\n<\/pre>\n<p>Passing an empty tuple for the second parameter uses the default constructor. If you want to pass constructor parameters for the <code>weird<\/code> object, you can put them inside the second tuple:<\/p>\n<pre>table.emplace(std::piecewise_construct,\r\n    std::forward_as_tuple(1),\r\n    std::forward_as_tuple(\"used to construct weird\"));\r\n<\/pre>\n<p>The <code>try_emplace<\/code> method uses its first parameter to construct the key and the remaining parameters to construct the mapped type. In our case, we want a default constructor, so we can just say nothing:<\/p>\n<pre>table.try_emplace(1);\r\n<\/pre>\n<p>If we want to pass constructor parameters for <code>weird<\/code>, we can pass them after the key:<\/p>\n<pre>table.try_emplace(1, \"parameters for constructing weird\");\r\n<\/pre>\n<p><b>Bonus chatter<\/b>: There are versions of these insertion and emplacement methods which take hints as the first parameter, ahead of the value or key. <a title=\"Speeding up the insertion of a sorted (or mostly-sorted) key list into a std::map or other ordered associative container\" href=\"https:\/\/devblogs.microsoft.com\/oldnewthing\/20230522-00\/?p=108226\"> We learned about hints some time ago<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Fancy emplacement.<\/p>\n","protected":false},"author":1069,"featured_media":111744,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[25],"class_list":["post-108916","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>Fancy emplacement.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/108916","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/users\/1069"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/comments?post=108916"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/108916\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media\/111744"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media?parent=108916"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=108916"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=108916"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}