{"id":7734,"date":"2018-05-05T08:16:05","date_gmt":"2018-05-05T08:16:05","guid":{"rendered":"http:\/\/blog.bachi.net\/?p=7734"},"modified":"2021-03-26T08:14:22","modified_gmt":"2021-03-26T08:14:22","slug":"c-copy-elision-and-return-value-optimization","status":"publish","type":"post","link":"https:\/\/blog.bachi.net\/?p=7734","title":{"rendered":"C++ Copy Elision and Return Value Optimization"},"content":{"rendered":"<ul>\n<li>RVO = Return Value Optimization<\/li>\n<li>NRVO = Named Return Value Optimization<\/li>\n<li>Copy Constructor (ctor)<\/li>\n<li>Copy Assignment Operator<\/li>\n<li>Move Constructor (ctor): <code>C::C(C&& other);<\/code><\/li>\n<li>Move Assignment Operator: <code>C& C::operator=(C&& other);<\/code><\/li>\n<li>Move Semantics<\/li>\n<li>Temporary<\/li>\n<li>Lvalue: object that occupies some identifiable location in memory (i.e. has an address)<\/li>\n<li>Rvalue: temporary register (i.e. has NOT address)<\/li>\n<li>Rvalue References: <code>std::string&& rrstr;<\/code><\/li>\n<li>Lvalue references: <code>std::string& ref;<\/code><\/li>\n<li>Reference Variables<\/li>\n<\/ul>\n<p><a href=\"https:\/\/eli.thegreenplace.net\/2011\/12\/15\/understanding-lvalues-and-rvalues-in-c-and-c\/\">Understanding lvalues and rvalues in C and C++<\/a><\/p>\n<p>Six User-Declared Special Member Functions<\/p>\n<ul>\n<li>Default constructor<\/li>\n<li>Copy constructor (ctor)<\/li>\n<li>Copy assignment operator<\/li>\n<li>Move Constructor (ctor)<\/li>\n<li>Move Assignment Operator<\/li>\n<li>Destructor<\/li>\n<\/ul>\n<p>C++ declares its remaining special member functions implicitly, if there are no user-declared functions.<\/p>\n<p>Vokabular<\/p>\n<ul>\n<li>pilfer = stehlen, entwenden, klauen, stibitzen<\/li>\n<li>elision = Weglassung, Auslassung<\/li>\n<\/ul>\n<p>Hitherto, copying has been the only means for transferring a state from one object to another.<br \/>\nAnd yet, in many real-world scenarios, you don\u2019t <strong>copy<\/strong> objects but <strong>move<\/strong> them.<br \/>\nNotwithstanding the conceptual difference between copying and moving, there\u2019s a practical difference too: Move operations tend to be faster than copying because they transfer an existing resource to a new destination, whereas copying requires the creation of a new resource from scratch.<\/p>\n<p>Bislang war das Kopieren das einzige Mittel, um einen Zustand von einem Objekt auf ein anderes zu \u00fcbertragen.<br \/>\nDoch in vielen realen Szenarien kopiert man Objekte nicht, sondern verschiebt sie.<br \/>\nUngeachtet des konzeptionellen Unterschieds zwischen Kopieren und Verschieben gibt es auch einen praktischen Unterschied: Verschiebeoperationen sind tendenziell schneller als Kopieren, weil sie eine bestehende Ressource an ein neues Ziel \u00fcbertragen, w\u00e4hrend beim Kopieren eine neue Ressource von Grund auf neu erstellt werden muss.<\/p>\n<p>\u00dcbersetzt mit www.DeepL.com\/Translator (kostenlose Version)<\/p>\n<p><a href=\"https:\/\/www.quora.com\/Is-there-any-gorgeous-way-for-C++-to-return-an-object-without-copying-it-in-memory\">Is there any gorgeous way for C++ to return an object without copying it in memory?<\/a><br \/>\n<a href=\"https:\/\/stackoverflow.com\/questions\/3721217\/returning-a-c-stdvector-without-a-copy\">Returning a c++ std::vector without a copy?<\/a><br \/>\n<a href=\"https:\/\/stackoverflow.com\/questions\/12953127\/what-are-copy-elision-and-return-value-optimization\">What are copy elision and return value optimization?<\/a><br \/>\n<a href=\"https:\/\/stackoverflow.com\/questions\/6233879\/move-or-named-return-value-optimization-nrvo\">Move or Named Return Value Optimization (NRVO)?<\/a><br \/>\n<a href=\"https:\/\/www.youtube.com\/watch?v=XYZ4exfEV2o\">What Is RVO And NRVO In C++?<\/a> (YouTube Video)<\/p>\n<p><a href=\"https:\/\/blog.smartbear.com\/c-plus-plus\/c11-tutorial-introducing-the-move-constructor-and-the-move-assignment-operator\/\">C++11 Tutorial: Introducing the Move Constructor and the Move Assignment Operator<\/a><\/p>\n<h3>cppreference.com<\/h3>\n<p><a href=\"http:\/\/en.cppreference.com\/w\/cpp\/language\/copy_elision\">Copy elision<\/a><br \/>\n<a href=\"http:\/\/en.cppreference.com\/w\/cpp\/language\/move_constructor\">Move constructors<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>RVO = Return Value Optimization NRVO = Named Return Value Optimization Copy Constructor (ctor) Copy Assignment Operator Move Constructor (ctor): C::C(C&#038;&#038; other); Move Assignment Operator: C&#038; C::operator=(C&#038;&#038; other); Move Semantics Temporary Lvalue: object that occupies some identifiable location in memory (i.e. has an address) Rvalue: temporary register (i.e. has NOT address) Rvalue References: std::string&#038;&#038; rrstr; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-7734","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/blog.bachi.net\/index.php?rest_route=\/wp\/v2\/posts\/7734","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.bachi.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.bachi.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.bachi.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.bachi.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=7734"}],"version-history":[{"count":20,"href":"https:\/\/blog.bachi.net\/index.php?rest_route=\/wp\/v2\/posts\/7734\/revisions"}],"predecessor-version":[{"id":11964,"href":"https:\/\/blog.bachi.net\/index.php?rest_route=\/wp\/v2\/posts\/7734\/revisions\/11964"}],"wp:attachment":[{"href":"https:\/\/blog.bachi.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=7734"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.bachi.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=7734"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.bachi.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=7734"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}