The Need for Move Semantics
Let’s look into the copying process as it worked prior to C++11. Suppose we start with something like this:
vector<string> vstr;// build up a vector of 20,000 strings, each of 1000 characters...vector<string> vstr_copy1(vstr); // make vstr_copy1 a copy of vstr
Both the vector class and the string class use dynamic memory allocation, so they will have copy constructors defined that use some version of new. To initialize the vstr_copy1 object, the vector<string> copy constructor will use new to allocate memory for 20,000 string objects, and each string object, in turn, will call the string copy constructor, which will use new to allocate memory for 1000 characters. Then all 20,000,000 characters will be copied from ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access