<?xml-stylesheet type="text/xsl" href="https://community.element14.com/cfs-file/__key/system/syndication/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>C++ moving and returning objects part 2: move and copy  (embedded friendly C++)</title><link>/technologies/code_exchange/b/blog/posts/c-moving-and-returning-objects-part-2-move-and-copy---rule-of-six</link><description>In object oriented designs, there are times when you replace an object by another. Example: when you store an object into an STL container. In a lot of those cases, you create an object, and add it. The object in the container is a cop...</description><dc:language>en-US</dc:language><generator>Telligent Community 12</generator><item><title>RE: C++ moving and returning objects part 2: move and copy  (embedded friendly C++)</title><link>https://community.element14.com/technologies/code_exchange/b/blog/posts/c-moving-and-returning-objects-part-2-move-and-copy---rule-of-six</link><pubDate>Tue, 13 Jan 2026 18:35:39 GMT</pubDate><guid isPermaLink="false">93d5dcb4-84c2-446f-b2cb-99731719e767:570ab178-4dbe-44b2-b7b2-c9077c66a62c</guid><dc:creator>Jan Cumps</dc:creator><slash:comments>0</slash:comments><description>&lt;p&gt;I used &lt;a href="https://en.wikipedia.org/wiki/Copy_elision#Summary" rel="noopener noreferrer nofollow" target="_blank" data-e14adj="t"&gt;wikipedia&amp;#39;s test case&lt;/a&gt; to check if C++ actually&amp;nbsp;manages to allocate an object, created in a function, directly in its final memory location: yes&lt;/p&gt;
&lt;p&gt;[embed:dc8ab71f-3b98-42d9-b0f6-e21e02a0f8e2:57e0d5ac-8dfb-48ce-add2-d3b2be1c8345:type=csharp&amp;text=%23include%20%3Ciostream%3E%0D%0A%0D%0Astruct%20MyStruct%20%7B%0D%0A%20%20%20%20MyStruct%28%29%20%3D%20default%3B%0D%0A%20%20%20%20MyStruct%28%5B%5Bmaybe_unused%5D%5D%20const%20MyStruct%26%20x%29%20%7B%0D%0A%20%20%20%20%20%20%20%20std%3A%3Acout%20%3C%3C%20%28%22A%20copy%20was%20made.%5Cn%22%29%3B%20%7D%0D%0A%7D%3B%0D%0A%0D%0AMyStruct%20f%28%29%20%7B%0D%0A%20%20%20%20return%20MyStruct%28%29%3B%0D%0A%7D%0D%0A%0D%0AMyStruct%20f_named%28%29%20%7B%0D%0A%20%20%20%20MyStruct%20m%3B%0D%0A%20%20%20%20return%20m%3B%0D%0A%7D%0D%0A%0D%0Aint%20main%28%29%20%7B%0D%0A%20%20%20%20std%3A%3Acout%20%3C%3C%20%28%22Hello%20World%21%5Cn%22%29%3B%0D%0A%20%20%20%20MyStruct%20obj%20%3D%20f%28%29%3B%0D%0A%20%20%20%20MyStruct%20obj2%20%3D%20f_named%28%29%3B%0D%0A%7D]&lt;/p&gt;
&lt;p&gt;obj and obj2 are 2 MyStruct objects.&amp;nbsp;Even though they are created inside functions ( f() and f_named() ), there is no copying when these 2 functions exit.&lt;br /&gt;They actually construct the object in the memory that&amp;#39;s allocated in main directly.&lt;/p&gt;
&lt;p&gt;C++ knows that the function&amp;nbsp;return values are temporary and not used after the function exits.&lt;br /&gt;So instead of creating a temporary object, and using a copy into obj or obj2 at return, it directly does the work in the&amp;nbsp;destination object.&lt;/p&gt;
&lt;p&gt;result: using a function to&amp;nbsp;factor an object comes without any runtime cost.&lt;/p&gt;
&lt;p&gt;&lt;img height="598" src="/resized-image/__size/1000x1196/__key/commentfiles/f7d226abd59f475c9d224a79e3f0ec07-570ab178-4dbe-44b2-b7b2-c9077c66a62c/pastedimage1768329465932v1.png" width="500" alt=" " /&gt;&lt;/p&gt;
&lt;div&gt;
&lt;div&gt;&lt;span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;&lt;img src="https://community.element14.com/aggbug?PostID=29083&amp;AppID=74&amp;AppType=Weblog&amp;ContentType=0" width="1" height="1"&gt;</description></item><item><title>RE: C++ moving and returning objects part 2: move and copy  (embedded friendly C++)</title><link>https://community.element14.com/technologies/code_exchange/b/blog/posts/c-moving-and-returning-objects-part-2-move-and-copy---rule-of-six</link><pubDate>Tue, 22 Jul 2025 19:07:33 GMT</pubDate><guid isPermaLink="false">93d5dcb4-84c2-446f-b2cb-99731719e767:570ab178-4dbe-44b2-b7b2-c9077c66a62c</guid><dc:creator>DAB</dc:creator><slash:comments>0</slash:comments><description>&lt;p&gt;Nice post Jan.&lt;/p&gt;&lt;img src="https://community.element14.com/aggbug?PostID=29083&amp;AppID=74&amp;AppType=Weblog&amp;ContentType=0" width="1" height="1"&gt;</description></item></channel></rss>