{"id":9881,"date":"2021-02-03T15:14:24","date_gmt":"2021-02-03T15:14:24","guid":{"rendered":"http:\/\/www.max-sperling.bplaced.net\/?p=9881"},"modified":"2024-10-01T13:11:30","modified_gmt":"2024-10-01T13:11:30","slug":"when-and-how-to-pass-smart-pointer","status":"publish","type":"post","link":"http:\/\/www.max-sperling.bplaced.net\/?p=9881","title":{"rendered":"When and how to pass smart pointer (C++)"},"content":{"rendered":"<p>This post is based on <a href=\"https:\/\/herbsutter.com\/2013\/06\/05\/gotw-91-solution-smart-pointer-parameters\/\">a post<\/a> from Herb Sutter.<\/p>\n<hr>\n<p><strong>When<\/strong><\/p>\n<pre>\r\nif (The ownership of the object isn't shared or moved &&\r\n    The lifetime of the object is guaranteed <sup>1<\/sup>)\r\n{\r\n    Pass the object itself by value, raw pointer or reference\r\n}\r\nelse\r\n{\r\n    Pass the smart pointer either by value or reference\r\n}\r\n<\/pre>\n<p><sup>1<\/sup> The passed object lives longer than it can be used by the receiver.<\/p>\n<hr>\n<p><strong>How<\/strong><\/p>\n<table>\n<colgroup>\n<col style=\"width:18%\">\n<col style=\"width:41%\">\n<col style=\"width:41\">\n  <\/colgroup>\n<tr>\n<th><\/th>\n<th>unique_ptr<\/th>\n<th>shared_ptr \/ weak_ptr<\/th>\n<\/tr>\n<tr>\n<td>By value<\/td>\n<td>To move the ownership<\/td>\n<td>To share the ownership<\/td>\n<\/tr>\n<tr>\n<td>By reference<\/td>\n<td colspan=\"2\" align=\"center\">To modify the smart pointer (e.g. reset)<\/td>\n<\/tr>\n<tr>\n<td>By const ref.<\/td>\n<td>Don&#8217;t, better use a raw pointer \/ ref.<\/td>\n<td>To share the ownership <sup>2<\/sup><\/td>\n<\/tr>\n<\/table>\n<p><sup>2<\/sup> Faster than by value, but you have to guarantee the lifetime while passing.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This post is based on a post from Herb Sutter. When if (The ownership of the object isn&#8217;t shared or<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false},"categories":[64],"tags":[],"_links":{"self":[{"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/posts\/9881"}],"collection":[{"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=9881"}],"version-history":[{"count":7,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/posts\/9881\/revisions"}],"predecessor-version":[{"id":17869,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/posts\/9881\/revisions\/17869"}],"wp:attachment":[{"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=9881"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=9881"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=9881"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}