std::move_o...::operator=(3)

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

πŸ’… std::move_only_function::operator= πŸ’…

okay so bestie, welcome to page std::move_o...::operator=(3) from the C++ Standard Library man pages β€” yes, the title's abbreviated like that, we don't make the rules πŸ’€. This is the assignment operator for std::move_only_function, and it's lowkey giving "assign a new target or yeet the old one" energy. Let's get into it 🫢


✨ NAME ✨

std::move_only_function::operator= β€” literally std::move_only_function::operator=, no cap πŸ’…

🌸 Synopsis 🌸

Here's the whole roster, bestie β€” four overloads, all since C++23:

move_only_function& operator=( move_only_function&& other );

(1) (since C++23)

move_only_function& operator=( const move_only_function& ) = delete;

(2) (since C++23)

move_only_function& operator=( std::nullptr_t ) noexcept;

(3) (since C++23)

template< class F >

(4) (since C++23)

move_only_function& operator=( F&& f );

So what does it actually do? It assigns a new target to std::move_only_function or destroys its target. That's the whole vibe check. Now let's break down each overload, because the details matter and we are not skipping them 😀

  1. Moves the target of other to *this, or destroys the target of *this (if any) if other is empty, by auto(std::move(other)).swap(*this). After move assignment, other is in a valid state with an unspecified value β€” so she's alive but her exact contents are a mystery, iconic behavior honestly πŸ™ˆ
  2. The copy assignment operator is deleted. std::move_only_function does not satisfy CopyAssignable. Not me getting blocked from copying, it's giving move-only main character energy πŸ’€
  3. Destroys the current target if it exists. After the call, *this is empty. Clean slate, no notes ✨
  4. Sets the target of *this to the callable f, or destroys the current target if f is a null function pointer, a null pointer to member function, or an empty std::move_only_function, as if by executing move_only_function(std::forward<F>(f)).swap(*this);. This overload participates in overload resolution only if the constructor of move_only_function from F participates in overload resolution. And heads up: the program is ill-formed or has undefined behavior if the selected constructor call is ill-formed or has undefined behavior β€” so like, pay attention bestie 😭

πŸŽ€ Parameters πŸŽ€

  • other β€” another std::move_only_function object to move the target of.
  • f β€” a callable object to initialize the new target with.

πŸ’– Return value πŸ’–

*this β€” that's it, that's the return value. periodt πŸ’…

πŸ¦‹ Notes πŸ¦‹

It is intentional not to require the move assignment operator to be noexcept to leave room for an allocator-aware move_only_function in future. So don't come for her about the missing noexcept, it's a whole future-proofing thing 🌈

Also: move_only_function can be assigned from std::in_place_type<Fn> given it can be constructed from that argument. cute, flexible, obsessed πŸŽ€

πŸ‘‘ See also πŸ‘‘

  • operator= assigns a new target (public member function of std::function<R(Args...)>)

and that's the page, bestie 🫢 source is http://cppreference.com, version 2024.06.10, and the footer name is std::move_o...::operator=(3) β€” go forth and assign your targets responsibly πŸ’œβœ¨

πŸ“¦ where this page comes from