std::function::operator=(3)

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

🌸 NAME 🌸

std::function::operator= β€” literally std::function::operator=. This is the std::function::operator=(3) page from the C++ Standard Libary manual, and it's your bestie-approved guide to assigning a new target to std::function πŸ’…βœ¨.

✨ Synopsis ✨

okay so here's the whole overload situation, copied exactly because we do not mess with signatures in this house 🫢:

function& operator=( const function& other );                (1)
       (since C++11)
          function& operator=( function&& other );                     (2)
       (since C++11)
          function& operator=( std::nullptr_t ) noexcept;              (3)
       (since C++11)
          template< class F >                                          (4)
       (since C++11)
          function& operator=( F&& f );
          template< class F >                                          (5)
       (since C++11)
          function& operator=( std::reference_wrapper<F> f ) noexcept;

std::function gets a whole new target assigned to it. That's the whole vibe of this page.

  1. (1) β€” Assigns a copy of target of other, as if by executing function(other).swap(*this);
  2. (2) β€” Moves the target of other to *this. other is in a valid state with an unspecified value.
  3. (3) β€” Drops the current target. *this is empty after the call.
  4. (4) β€” Sets the target of *this to the callable f, as if by executing function(std::forward<F>(f)).swap(*this);. This operator does not participate in overload resolution unless f is Callable for argument types Args... and return type R.
  5. (5) β€” Sets the target of *this to a copy of f, as if by executing function(f).swap(*this);

All five are marked (since C++11) in the synopsis above, bestie ⭐.

πŸŽ€ Parameters πŸŽ€

  • other β€” another std::function object to copy the target of.
  • f β€” a callable to initialize the target with.

πŸ¦‹ Type requirements πŸ¦‹

  • F must meet the requirements of Callable.

πŸ’– Return value πŸ’–

*this β€” that's the whole return value, no cap 😌.

πŸ”₯ Notes πŸ”₯

Even before allocator support was removed from std::function in C++17, these assignment operators use the default allocator rather than the allocator of *this or the allocator of other (see LWG issue 2386). That's a real note, not me being dramatic for once πŸ™ˆ.

🧚 Defect reports 🧚

The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

  • DR LWG 2132 β€” Applied to: C++11. Behavior as published: the overload taking a Callable object might be ambiguous. Correct behavior: constrained.
  • DR LWG 2401 β€” Applied to: C++11. Behavior as published: assignment operator from std::nullptr_t not required. Correct behavior: required to be noexcept.

πŸ‘‘ See also πŸ‘‘

  • operator= β€” replaces or destroys the target (C++23) (public member function of std::move_only_function)
  • assign β€” assigns a new target (removed in C++17) (public member function)

okay that's the whole assignment saga, bestie πŸ’– source: http://cppreference.com Β· version/date: 2024.06.10 Β· page: std::function::operator=(3) β€” now go assign something iconic πŸ’…βœ¨

πŸ“¦ where this page comes from