std::function::operator=(3)
πΈ 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)β Assigns a copy of target ofother, as if by executingfunction(other).swap(*this);(2)β Moves the target ofotherto*this.otheris in a valid state with an unspecified value.(3)β Drops the current target.*thisis empty after the call.(4)β Sets the target of*thisto the callablef, as if by executingfunction(std::forward<F>(f)).swap(*this);. This operator does not participate in overload resolution unlessfis Callable for argument typesArgs...and return typeR.(5)β Sets the target of*thisto a copy off, as if by executingfunction(f).swap(*this);
All five are marked (since C++11) in the synopsis above, bestie β.
π Parameters π
otherβ anotherstd::functionobject to copy the target of.fβ a callable to initialize the target with.
π¦ Type requirements π¦
Fmust 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 fromstd::nullptr_tnot required. Correct behavior: required to benoexcept.
π See also π
operator=β replaces or destroys the target (C++23) (public member function ofstd::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
- 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