{"id":109481,"date":"2024-03-06T07:00:00","date_gmt":"2024-03-06T15:00:00","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/oldnewthing\/?p=109481"},"modified":"2024-03-06T07:02:02","modified_gmt":"2024-03-06T15:02:02","slug":"20240306-00","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20240306-00\/?p=109481","title":{"rendered":"How do I make an expression non-movable? What&#8217;s the opposite of <CODE>std::move<\/CODE>?"},"content":{"rendered":"<p>You can use <code>std::move<\/code> to indicate that a value can be moved. But what can you do to indicate that something movable cannot be moved after all?<\/p>\n<p>For example, maybe you want to force the copy assignment operator to be used instead of the move assignment operator:<\/p>\n<pre>extern Thing GetThing();\r\n\r\nThing thing;\r\n\r\n\/\/ this uses the move assignment operator\r\nthing = GetThing();\r\n<\/pre>\n<p>You might want to force the copy assignment operator because the copy assignment operator comes with additional statistics or bookkeeping. How do you make something non-movable?<\/p>\n<p>You can do it by const-ifying the thing, so that the only available option is to copy it.<\/p>\n<pre>template&lt;typename T&gt;\r\nstd::remove_reference_t&lt;T&gt; const&amp; no_move(T&amp;&amp; t)\r\n{\r\n    return t;\r\n}\r\n<\/pre>\n<p>The <code>no_move<\/code> function takes any kind of reference, and then returns a <code>const<\/code> reference to that referred-to thing.<\/p>\n<pre>extern std::vector&lt;int&gt; make_vector();\r\n\r\n\/\/ Force copy assignment, not move assignment.\r\nv = no_move(make_vector());\r\n<\/pre>\n<p><b>Bonus chatter<\/b>: Note that the following similar-looking code is not a move operation:<\/p>\n<pre>std::vector&lt;int&gt; v = make_vector();\r\n<\/pre>\n<p>The above code does not construct <code>v<\/code> by moving the result of <code>make_vector<\/code>. Rather, the above code uses copy elision, and the return value of <code>make_vector<\/code> is placed directly into <code>v<\/code> without any copying or moving at all.<\/p>\n<p>But the same trick can be used to suppress copy elision.<\/p>\n<pre>\/\/ Force copy constructor, not <span style=\"text-decoration: line-through;\">move constructor<\/span> copy elision.\r\nstd::vector&lt;int&gt; v = no_move(make_vector());\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>You can turn it into a const thing so it&#8217;s no longer movable, in a conventional sense.<\/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-109481","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>You can turn it into a const thing so it&#8217;s no longer movable, in a conventional sense.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/109481","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=109481"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/109481\/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=109481"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=109481"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=109481"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}