{"id":1616,"date":"2021-07-09T17:26:08","date_gmt":"2021-07-10T00:26:08","guid":{"rendered":"http:\/\/66.94.114.210\/?p=1616"},"modified":"2021-07-09T17:30:31","modified_gmt":"2021-07-10T00:30:31","slug":"whether-should-we-call-stdmove","status":"publish","type":"post","link":"https:\/\/nanzhou.cc\/index.php\/2021\/07\/09\/whether-should-we-call-stdmove\/","title":{"rendered":"Whether Should We Call std::move on std::unique_ptr"},"content":{"rendered":"\n<p>If a function&nbsp;<strong>takes<\/strong>&nbsp;a&nbsp;<code>std::unique_ptr&lt;&gt;<\/code>, that means it takes ownership of the argument. Callers need to use&nbsp;<code>std::move()<\/code>&nbsp;to indicate that they&#8217;re passing ownership if the object being passed is not a temporary:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"cpp\" class=\"language-cpp\">\/\/ Foo() takes ownership of |bar|.\nvoid Foo(std::unique_ptr&lt;Bar> bar);\n\n...\nstd::unique_ptr&lt;Bar> bar_ptr(new Bar());\nFoo(std::move(bar_ptr));          \/\/ After this statement, |bar_ptr| is null.\nFoo(std::unique_ptr&lt;Bar>(new Bar()));  \/\/ No need to use std::move() on temporaries. You can do so but it prevents copy elision<\/code><\/pre>\n\n\n\n<p>If a function&nbsp;<strong>returns<\/strong>&nbsp;a&nbsp;<code>std::unique_ptr&lt;&gt;<\/code>, that means the caller takes ownership of the returned object. Usage of&nbsp;<code>std::move()<\/code>&nbsp;while returning an object is only needed if the return type of the function differs from the type of the local variable.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"cpp\" class=\"language-cpp\">class Base { ... };\nclass Derived : public Base { ... };\n\n\/\/ Foo takes ownership of |base|, and the caller takes ownership of the returned\n\/\/ object.\nstd::unique_ptr&lt;Base&gt; Foo(std::unique_ptr&lt;Base&gt; base) {\n  if (cond) {\n    return base;                           \/\/ Transfers ownership of |base| back to\n                                           \/\/ the caller.\n  }\n\n  \/\/ Note that on these next codepaths, |base| is deleted on exit.\n  if (cond2) {\n    return std::unique_ptr&lt;Base&gt;(new Base()));  \/\/ No std::move() necessary on temporaries.\n  }\n  std::unique_ptr&lt;Derived&gt; derived(new Derived());\n  return std::move(derived);               \/\/ Note that std::move() is necessary because\n                                           \/\/ type of |derived| is different from the return\n                                           \/\/ type of the function.\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>If a function&nbsp;takes&nbsp;a&nbsp;std::unique_ptr&lt;&gt;, that means it takes ownership of the argument. Callers need to use&nbsp;std::move()&nbsp;to indicate that they&#8217;re passing ownership if the object being passed is not a temporary: If a function&nbsp;returns&nbsp;a&nbsp;std::unique_ptr&lt;&gt;, that means the caller takes ownership of the returned object. Usage of&nbsp;std::move()&nbsp;while returning an object is only needed if the return type of&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6,5],"tags":[],"class_list":["post-1616","post","type-post","status-publish","format-standard","hentry","category-c","category-proglang"],"_links":{"self":[{"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/posts\/1616","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/comments?post=1616"}],"version-history":[{"count":4,"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/posts\/1616\/revisions"}],"predecessor-version":[{"id":1620,"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/posts\/1616\/revisions\/1620"}],"wp:attachment":[{"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/media?parent=1616"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/categories?post=1616"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/tags?post=1616"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}