std::is_mov...assignable,(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

πŸ’– NAME πŸ’–

Okay bestie, welcome to the std::is_mov...assignable,(3) page from the C++ Standard Library manual β€” the one all about move-assignability type traits. It covers std::is_move_assignable, std::is_trivially_move_assignable, and std::is_nothrow_move_assignable. No cap, these are the vibe-checkers for β€œcan I move-assign this type?” πŸ’…


✨ Synopsis ✨

Defined in header <type_traits>:

template< class T >                  (1) (since C++11)
struct is_move_assignable;
template< class T >                  (2) (since C++11)
struct is_trivially_move_assignable;
template< class T >                  (3) (since C++11)
struct is_nothrow_move_assignable;

Here’s the value table, bestie: the T is a referenceable type column gives the trait expression below, and the T is not a referenceable type column is false (the extracted text shows false on the (2) row). Table translation era πŸ’–

  • (1) β€” std::is_assignable<T, T&&>::value
  • (2) β€” std::is_trivially_assignable<T, T&&>::value
  • (3) β€” std::is_nothrow_assignable<T, T&&>::value
Behavior undefined alert 🚨 If T is not a complete type, (possibly cv-qualified) void, or an array of unknown bound, the behavior is undefined.
Another UB warning πŸ’€ If an instantiation of a template above depends, directly or indirectly, on an incomplete type, and that instantiation could yield a different result if that type were hypothetically completed, the behavior is undefined.
Specialization warning πŸ™ˆ If the program adds specializations for any of the templates described on this page, the behavior is undefined.

🫢 Member constants 🫢

  • value β€” true if T is move-assignable, false otherwise. It’s [static] β€” public static member constant. Iconic little boolean.

🌸 Member functions 🌸

  • operator bool β€” converts the object to bool, returns value (public member function).
  • operator() β€” returns value (C++14) (public member function).

πŸŽ€ Member types πŸŽ€

  • value_type β€” bool
  • type β€” std::integral_constant<bool, value>

🧚 Possible implementation 🧚

template<class T>
struct is_move_assignable
    : std::is_assignable<typename std::add_lvalue_reference<T>::type,
                         typename std::add_rvalue_reference<T>::type>
{};

template<class T>
struct is_trivially_move_assignable
    : std::is_trivially_assignable<typename std::add_lvalue_reference<T>::type,
                                   typename std::add_rvalue_reference<T>::type> {};

template<class T>
struct is_nothrow_move_assignable
    : std::is_nothrow_assignable<typename std::add_lvalue_reference<T>::type,
                                 typename std::add_rvalue_reference<T>::type> {};

πŸ”₯ Notes πŸ”₯

The trait std::is_move_assignable is less strict than MoveAssignable because it does not check the type of the result of the assignment (which, for a MoveAssignable type, must be T&), nor the semantic requirement that the target’s value after the assignment is equivalent to the source’s value before the assignment.

The type does not have to implement a move assignment operator in order to satisfy this trait; see MoveAssignable for details.


πŸ’œ Example πŸ’œ

// Run this code

#include <iostream>
#include <string>
#include <type_traits>

struct Foo { int n; };

struct NoMove
{
    // prevents implicit declaration of default move assignment operator
    // however, the class is still move-assignable because its
    // copy assignment operator can bind to an rvalue argument
    NoMove& operator=(const NoMove&) { return *this; }
};

int main()
{
    std::cout << std::boolalpha
              << "std::string is nothrow move-assignable? "
              << std::is_nothrow_move_assignable<std::string>::value << '\n'
              << "int[2] is move-assignable? "
              << std::is_move_assignable<int[2]>::value << '\n'
              << "Foo is trivially move-assignable? "
              << std::is_trivially_move_assignable<Foo>::value << '\n'
              << "NoMove is move-assignable? "
              << std::is_move_assignable<NoMove>::value << '\n'
              << "NoMove is nothrow move-assignable? "
              << std::is_nothrow_move_assignable<NoMove>::value << '\n';
}

πŸ“ Output: πŸ“

std::string is nothrow move-assignable? true
int[2] is move-assignable? false
Foo is trivially move-assignable? true
NoMove is move-assignable? true
NoMove is nothrow move-assignable? false

🌈 See also 🌈

  • is_assignable (C++11), is_trivially_assignable (C++11), is_nothrow_assignable (C++11) β€” checks if a type has an assignment operator for a specific argument (class template).
  • is_copy_assignable (C++11), is_trivially_copy_assignable (C++11), is_nothrow_copy_assignable (C++11) β€” checks if a type has a copy assignment operator (class template).

Source/version/date energy: http://cppreference.com, 2024.06.10, page std::is_mov...assignable,(3). That’s the whole manual page, bestie β€” go move-assign something responsibly! πŸ’–βœ¨πŸŽ€

πŸ“¦ where this page comes from