std::move_o...::operator=(3)
π 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 π€
- Moves the target of
otherto*this, or destroys the target of*this(if any) ifotheris empty, byauto(std::move(other)).swap(*this). After move assignment,otheris in a valid state with an unspecified value β so she's alive but her exact contents are a mystery, iconic behavior honestly π - The copy assignment operator is deleted.
std::move_only_functiondoes not satisfyCopyAssignable. Not me getting blocked from copying, it's giving move-only main character energy π - Destroys the current target if it exists. After the call,
*thisis empty. Clean slate, no notes β¨ - Sets the target of
*thisto the callablef, or destroys the current target iffis a null function pointer, a null pointer to member function, or an emptystd::move_only_function, as if by executingmove_only_function(std::forward<F>(f)).swap(*this);. This overload participates in overload resolution only if the constructor ofmove_only_functionfromFparticipates 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β anotherstd::move_only_functionobject 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 ofstd::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
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms