liveBook · Manning

Move constructor

As explained in Learn C++ by Example

A move constructor is a constructor that initializes an object by taking ownership of resources from another object, typically an rvalue.

A move constructor initializes an object from an rvalue by reusing its resources instead of copying them. For std::vector, this can transfer ownership of the element storage from a temporary vector. section 2.2.3

Generating Pascal’s triangle

triangle.push_back(get_next_row(triangle.back())) passes a temporary row to push_back. The T&& overload can move-construct the new vector, avoiding the copy required by push_back(const T&). section 2.2.3

The important rules are:

  • std::move casts a named value back to an rvalue; it does not itself move anything. section 2.2.3
  • After moving, the source object remains valid but has an unspecified state and is generally no longer useful. section 2.2.3

How Learn C++ by Example covers it

How deep it goes
Explains rvalue overload selection and resource reuse, but only sketches move semantics and perfect forwarding.
Examples
Demonstrates temporary row vectors while generating Pascal’s triangle with push_back and emplace_back.
What it assumes
Introduces the distinction between named lvalues and rvalues while assuming familiarity with constructors and vectors.
What you can do after
Apply it via std::vector insertion while recognizing when overload resolution selects moving instead of copying.
How it connects
Connects move constructors with rvalue references, std::move, perfect forwarding, copy constructors, and std::vector.

Also explained in

Compare how 2 books explain move constructor →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn C++ by Example. The section links point to the parts of the book it draws on.