liveBook · Manning

Move assignment operator

As explained in Learn C++ by Example

A move assignment operator is a class member function that replaces an object's state by transferring resources from another object, typically an rvalue.

A move assignment operator replaces an object’s state by transferring resources from another object, typically an rvalue. Its signature follows the special-member pattern `X& operator=(X&&)`. Alongside the destructor, copy constructor, copy assignment operator, and move constructor, it forms part of the rule of five. section 6.1.4 section 6.1.1

Use these relationships to reason about whether move assignment is available and useful:

  • Defining none of the special members lets the compiler default all six, following the rule of zero. section 6.1.1
  • Defining a destructor, copy operation, or resource-management behavior may require explicitly considering move assignment and the other members under the rule of five. section 6.1.4
  • When a type cannot be moved, vector `push_back` may use copying instead, losing a move-based optimization opportunity. section 6.1.4
Listing 6.10 Type traits to check for special member functions
#include <type_traits>
void check_properties()
{
    ...                                               #1
    static_assert(
        !std::is_default_constructible_v<Race::Blob>
    );                                                #2
    static_assert(
         std::is_destructible_v<Race::Blob>
    );                                                #2
    static_assert(
        !std::is_copy_constructible_v<Race::Blob>
    );                                                #2
    static_assert(
        !std::is_copy_assignable_v<Race::Blob>
    );                                                #2
    static_assert(
        !std::is_move_constructible_v<Race::Blob>
    );                                                #2
    static_assert(
        !std::is_move_assignable_v<Race::Blob>
    );                                                #2
    static_assert(
         std::has_virtual_destructor_v<Race::Blob>
    );                                                #3
}

Inspect compile-time checks that determine whether a type is move-assignable, alongside its constructibility, copyability, and destructibility. section 6.1.4

constexpr void push_back( const T& value );
constexpr void push_back( T&& value ); 

Compare copying and moving at a library boundary: `push_back` accepts either a const reference or an rvalue reference. section 6.1.4

How Learn C++ by Example covers it

How deep it goes
Defines its special-member role and tests availability with type traits, but stops short of implementing a resource-owning operator.
Examples
Uses vector `push_back`, an abstract `Blob`, and `std::is_move_assignable_v` assertions as concrete examples.
What it assumes
Assumes basic C++ classes and references while introducing special-member categories and type-trait queries.
What you can do after
Evaluate whether a class supports move assignment and predict when library code falls back to copying.
How it connects
Connects move assignment with move construction, copy assignment, destructors, the rule of five, the rule of zero, and vector.

Also explained in

Compare how 2 books explain move assignment operator →

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.