{"id":15974,"date":"2022-10-13T18:48:27","date_gmt":"2022-10-13T13:18:27","guid":{"rendered":"https:\/\/jassweb.com\/solved\/solved-why-not-destory-after-calling-stdmove-closed\/"},"modified":"2022-10-13T18:48:27","modified_gmt":"2022-10-13T13:18:27","slug":"solved-why-not-destory-after-calling-stdmove-closed","status":"publish","type":"post","link":"https:\/\/jassweb.com\/solved\/solved-why-not-destory-after-calling-stdmove-closed\/","title":{"rendered":"[Solved] Why not destory after calling std::move? [closed]"},"content":{"rendered":"<p> [ad_1]<br \/>\n<\/p>\n<div id=\"answer-64335967\" class=\"answer js-answer accepted-answer js-accepted-answer\" data-answerid=\"64335967\" data-parentid=\"64331419\" data-score=\"0\" data-position-on-page=\"1\" data-highest-scored=\"1\" data-question-has-accepted-highest-score=\"1\" itemprop=\"acceptedAnswer\" itemscope itemtype=\"https:\/\/schema.org\/Answer\">\n<div class=\"post-layout\">\n<div class=\"votecell post-layout--left\"><\/div>\n<div class=\"answercell post-layout--right\">\n<div class=\"s-prose js-post-body\" itemprop=\"text\">\n<p>So move semantics is basically doing a shallow copy from an element that is about to die (go out of scope).<\/p>\n<p>Say that we have object, <code>dying_obj<\/code>, that we know is about to go out of scope (and hence have its destructor called), we can change its type to an <code>rvalue<\/code> with <code>std::move<\/code> like so:<\/p>\n<pre class=\"lang-c prettyprint-override\"><code>other_obj = std::move(dying_obj);\n<\/code><\/pre>\n<p><code>std::move<\/code> is just a type cast. It has no run-time effect other than how its type is interpreted by the compiler for things like overloaded function resolution. In this case, constructor overloads.<\/p>\n<p>It&#8217;s important the understand that this really is just a type flag, and the <em><strong>convention<\/strong><\/em> is that it refers to an object out-of-scope for its caller.<\/p>\n<p>BUT.. to hammer the point home that <code>rvalues<\/code> and <code>lvalues<\/code> are just little flags, we could abuse these flags for other purposes (don&#8217;t ever do this!):<\/p>\n<pre class=\"lang-c prettyprint-override\"><code>void increment (int &amp; i)\n{\n    ++i;\n}\n\nvoid increment (int &amp;&amp; i)\n{\n    --i;\n}\n\nint main ()\n{\n    \/\/ count up to 10:\n    for (int i = 0; i &lt; 10; increment(i))\n        std::cout &lt;&lt; i &lt;&lt; \" \";\n\n    \/\/ count down from 20:\n    for (int i = 20; i &gt; 0; increment(std::move(i)))\n        std::cout &lt;&lt; i &lt;&lt; \" \";\n}\n<\/code><\/pre>\n<p>The above code uses an <code>lvalue<\/code> as an &#8220;add&#8221; type and the <code>rvalue<\/code> as a minus type. Very dumb, but also perfectly logical, it works correctly and with defined behaviour.<\/p>\n<p>Getting back to the real world: when you call a function\/method with an <code>rvalue<\/code> object, like <code>foo(std::move(obj))<\/code>, you&#8217;re signalling to the function that it can do as it likes with that object because it won&#8217;t be used by the caller any more. That&#8217;s the <em><strong>convention<\/strong><\/em> for what the signal means, but it&#8217;s just a signal. You can certainly rely on the STL library to follow this convention everywhere, and probably every single library that&#8217;s ever used by sane people.<\/p>\n<p>Standard procedure is to do the move copy as efficiently as possible. Sadly we&#8217;re forced to copy all the values, but we&#8217;re not forced to copy all of the content that a pointer points to (this is where we do a shallow copy instead). For the pointers, we really must be careful to make sure that the destructor of <code>dying_obj<\/code> won&#8217;t delete any of its allocations &#8211; there&#8217;s lots of ways to do this, actually, but the simplest is just to change any pointers to <code>nullptr<\/code> after the copy. We could change the other values of <code>dying_obj<\/code>, but we don&#8217;t need to and it takes time, so it&#8217;s typical to leave it as it is.<\/p>\n<p>So it&#8217;s important to understand that the <code>std::move(obj)<\/code> doesn&#8217;t invoke a destruction of the <code>obj<\/code> &#8211; it will still go out of scope at the same place, so at that point its destructor will be called &#8211; which, if we&#8217;ve done our job correctly, will have no effect at all.<\/p>\n<p>As for your precise problem:<\/p>\n<pre class=\"lang-c prettyprint-override\"><code>pointer p1;\np1.val = 3;\n*p1.my_pointer = 3;\npointer p2(std::move(p1));\nprintf(\"%d\\n\", p1.val);\nprintf(\"%d\\n\", p2.val);\n<\/code><\/pre>\n<p>With <code>std::move(p1)<\/code> you&#8217;re communicating to the constructor that <code>p1<\/code> won&#8217;t be used again, so it can pull out its contents (do the shallow copy), but then on the next line you show that you&#8217;re still using <code>p1<\/code>:<\/p>\n<pre class=\"lang-c prettyprint-override\"><code>printf(\"%d\\n\", p1.val);\n<\/code><\/pre>\n<p>So you&#8217;re breaking your promise.<\/p>\n<\/p><\/div>\n<div class=\"mt24\"><\/div>\n<\/div>\n<p>            <span class=\"d-none\" itemprop=\"commentCount\">1<\/span> <\/p><\/div>\n<\/div>\n<p>[ad_2]<\/p>\n<p>solved Why not destory after calling std::move? [closed] <\/p>\n","protected":false},"excerpt":{"rendered":"<p>[ad_1] So move semantics is basically doing a shallow copy from an element that is about to die (go out of scope). Say that we have object, dying_obj, that we know is about to go out of scope (and hence have its destructor called), we can change its type to an rvalue with std::move like &#8230; <a title=\"[Solved] Why not destory after calling std::move? [closed]\" class=\"read-more\" href=\"https:\/\/jassweb.com\/solved\/solved-why-not-destory-after-calling-stdmove-closed\/\" aria-label=\"More on [Solved] Why not destory after calling std::move? [closed]\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[320],"tags":[324],"class_list":["post-15974","post","type-post","status-publish","format-standard","hentry","category-solved","tag-c"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>[Solved] Why not destory after calling std::move? [closed] - JassWeb<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/jassweb.com\/solved\/solved-why-not-destory-after-calling-stdmove-closed\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"[Solved] Why not destory after calling std::move? [closed] - JassWeb\" \/>\n<meta property=\"og:description\" content=\"[ad_1] So move semantics is basically doing a shallow copy from an element that is about to die (go out of scope). Say that we have object, dying_obj, that we know is about to go out of scope (and hence have its destructor called), we can change its type to an rvalue with std::move like ... Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jassweb.com\/solved\/solved-why-not-destory-after-calling-stdmove-closed\/\" \/>\n<meta property=\"og:site_name\" content=\"JassWeb\" \/>\n<meta property=\"article:published_time\" content=\"2022-10-13T13:18:27+00:00\" \/>\n<meta name=\"author\" content=\"Kirat\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Kirat\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-why-not-destory-after-calling-stdmove-closed\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-why-not-destory-after-calling-stdmove-closed\/\"},\"author\":{\"name\":\"Kirat\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\"},\"headline\":\"[Solved] Why not destory after calling std::move? [closed]\",\"datePublished\":\"2022-10-13T13:18:27+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-why-not-destory-after-calling-stdmove-closed\/\"},\"wordCount\":483,\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"keywords\":[\"c++\"],\"articleSection\":[\"Solved\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-why-not-destory-after-calling-stdmove-closed\/\",\"url\":\"https:\/\/jassweb.com\/solved\/solved-why-not-destory-after-calling-stdmove-closed\/\",\"name\":\"[Solved] Why not destory after calling std::move? [closed] - JassWeb\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#website\"},\"datePublished\":\"2022-10-13T13:18:27+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-why-not-destory-after-calling-stdmove-closed\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/jassweb.com\/solved\/solved-why-not-destory-after-calling-stdmove-closed\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-why-not-destory-after-calling-stdmove-closed\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/jassweb.com\/solved\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"[Solved] Why not destory after calling std::move? [closed]\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/jassweb.com\/solved\/#website\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"name\":\"JassWeb\",\"description\":\"Build High-quality Websites\",\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/jassweb.com\/solved\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\",\"name\":\"Jass Web\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"contentUrl\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"width\":693,\"height\":132,\"caption\":\"Jass Web\"},\"image\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\",\"name\":\"Kirat\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750\",\"contentUrl\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750\",\"caption\":\"Kirat\"},\"sameAs\":[\"http:\/\/jassweb.com\"],\"url\":\"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"[Solved] Why not destory after calling std::move? [closed] - JassWeb","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/jassweb.com\/solved\/solved-why-not-destory-after-calling-stdmove-closed\/","og_locale":"en_US","og_type":"article","og_title":"[Solved] Why not destory after calling std::move? [closed] - JassWeb","og_description":"[ad_1] So move semantics is basically doing a shallow copy from an element that is about to die (go out of scope). Say that we have object, dying_obj, that we know is about to go out of scope (and hence have its destructor called), we can change its type to an rvalue with std::move like ... Read more","og_url":"https:\/\/jassweb.com\/solved\/solved-why-not-destory-after-calling-stdmove-closed\/","og_site_name":"JassWeb","article_published_time":"2022-10-13T13:18:27+00:00","author":"Kirat","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Kirat","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jassweb.com\/solved\/solved-why-not-destory-after-calling-stdmove-closed\/#article","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/solved-why-not-destory-after-calling-stdmove-closed\/"},"author":{"name":"Kirat","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31"},"headline":"[Solved] Why not destory after calling std::move? [closed]","datePublished":"2022-10-13T13:18:27+00:00","mainEntityOfPage":{"@id":"https:\/\/jassweb.com\/solved\/solved-why-not-destory-after-calling-stdmove-closed\/"},"wordCount":483,"publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"keywords":["c++"],"articleSection":["Solved"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/jassweb.com\/solved\/solved-why-not-destory-after-calling-stdmove-closed\/","url":"https:\/\/jassweb.com\/solved\/solved-why-not-destory-after-calling-stdmove-closed\/","name":"[Solved] Why not destory after calling std::move? [closed] - JassWeb","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/#website"},"datePublished":"2022-10-13T13:18:27+00:00","breadcrumb":{"@id":"https:\/\/jassweb.com\/solved\/solved-why-not-destory-after-calling-stdmove-closed\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jassweb.com\/solved\/solved-why-not-destory-after-calling-stdmove-closed\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/jassweb.com\/solved\/solved-why-not-destory-after-calling-stdmove-closed\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/jassweb.com\/solved\/"},{"@type":"ListItem","position":2,"name":"[Solved] Why not destory after calling std::move? [closed]"}]},{"@type":"WebSite","@id":"https:\/\/jassweb.com\/solved\/#website","url":"https:\/\/jassweb.com\/solved\/","name":"JassWeb","description":"Build High-quality Websites","publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/jassweb.com\/solved\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/jassweb.com\/solved\/#organization","name":"Jass Web","url":"https:\/\/jassweb.com\/solved\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/","url":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","contentUrl":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","width":693,"height":132,"caption":"Jass Web"},"image":{"@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31","name":"Kirat","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/","url":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750","contentUrl":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750","caption":"Kirat"},"sameAs":["http:\/\/jassweb.com"],"url":"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/"}]}},"_links":{"self":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/15974","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/comments?post=15974"}],"version-history":[{"count":0,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/15974\/revisions"}],"wp:attachment":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/media?parent=15974"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/categories?post=15974"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/tags?post=15974"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}