
Understanding Copy and Move Semantics in C++
Explore the concepts of copy construction, assignment, move semantics, and pointer-like classes in C++. Learn the differences between values and pointers, swap functions, and the semantics of lvalues and rvalues. Discover how to manage resources and utilize type-specific swap functions effectively.
Download Presentation

Please find below an Image/Link to download the presentation.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.
You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.
E N D
Presentation Transcript
E81 CSE 428S: Multi-Paradigm Programming in C++ Copy and Move Semantics Department of Computer Science & Engineering Washington University, St. Louis MO Chris Gill and Oren Bell {cdgill, oren.bell}@wustl.edu 1
Copy Constructor/Assignment, Destructor Can prevent copy construction and/or assignment Declaring either or both of them with = delete E.g., unique_ptr cannot be copy assigned or constructed Generally speaking, any class with a non-empty destructor needs copy construction/assignment and vice versa (so called Rule of Three ) Declaring a copy constructor prevents compiler from synthesizing a default constructor automatically If you need it to do that, you need to ask for that by declaring a default constructor with = default (but not defining it) Default constructor does member-wise assignment CSE 428S Multi-Paradigm Programming in C++ 2
Classes that act like Values vs. Pointers Class that acts like a value has its own copy of a resource that it manages (a deck of cards, etc.) Needs copy constructor, copy assignment operator, destructor Copy assignment destroys old value, copies in new one Class that acts like a pointer may have copy semantics E.g., does a shallow copy of the resource by duplicating alias May involve reference counting, etc. (as with shared_ptr) Class that acts like a pointer may move vs. copy E.g., if there is a single instance that is owned and ownership is handed off between instances (as do unique_ptr, thread) CSE 428S Multi-Paradigm Programming in C++ 3
Swap functions vs. std::swap Reordering algorithms invoke swap operations E.g., std::sort swaps values in an array to rearrange them If a class declares/defines a type-specific swap function (or member function) that type-specific version is used Otherwise std::swap is used, which uses copy assignment Use of type-specific swap is often preferable E.g., to do a shallow swap or ownership-transferring swap Similarly, class level swap functions should use members type specific swap functions rather than std::swap where possible Use of type-specific swap in assignment operators If type-specific swap operation is provided then assignment can use copy and swap to ensure the old state is destroyed E.g., in the copy assignment operator you can pass in rhs by value and then do just: swap(*this, rhs); return *this; CSE 428S Multi-Paradigm Programming in C++ 4
Semantics of lvalues, rvalues, references Variables, etc. are persistent lvalues that can appear on the left hand side of an assignment operator lvalue references are declared with a single ampersand (&) E.g., int j = 2; int &k = j; // lvalue j, lvalue ref k Expressions are ephemeral rvalues that can appear on the right hand side of an assignment operator rvalue references are declared with two ampersands (&&) E.g., &&r = j * 3; // rvalue ref r Pass by value uses rvalues (or lvalues interpreted as rvalues) void func(int j); Pass by reference uses lvalues void func(int &j); Additional reading found in LLM 4.1 and 13.6 CSE 428S Multi-Paradigm Programming in C++ 5
Move Semantics Library function std::move lets us treat lvalue as if it were an rvalue (promises to only assign or destroy it) E.g., int &&r = std::move(j); // not safe to use j now Essentially, the lvalue s implementation has been moved away Move constructor Steals the implementation of the passed rvalue to construct the object on which the constructor is called Move assignment Steals the implementation of the passed rvalue and assigns it to the object on which the assignment operator is called Moved-from object must be destructible That is, it must be able to be destroyed safely even after its implementation has been stolen and given to another object CSE 428S Multi-Paradigm Programming in C++ 6
Studio 11 Explore how using = default may be needed to ask compiler to synthesize a particular constructor Creating a class that acts mostly like a value, with copy construction/assignment and a destructor Look at how move semantics can be used with smart pointers like unique_ptr Studios 0 through 11 are due 11:59pm Monday October 16th (the night before Exam 0) Submit as soon as each is done so you get feedback and can resubmit any that may be marked incomplete CSE 428S Multi-Paradigm Programming in C++ 7