Namespaces
Variants
Views
Actions

std::function<R(Args...)>::operator=

From cppreference.com
< cpp‎ | utility‎ | functional‎ | function
 
 
Utilities library
Language support
Type support (basic types, RTTI)
Library feature-test macros (C++20)
Dynamic memory management
Program utilities
Coroutine support (C++20)
Variadic functions
Debugging support
(C++26)
Three-way comparison
(C++20)
(C++20)(C++20)(C++20)
(C++20)(C++20)(C++20)
General utilities
Date and time
Function objects
Formatting library (C++20)
(C++11)
Relational operators (deprecated in C++20)
Integer comparison functions
(C++20)(C++20)(C++20)   
(C++20)
Swap and type operations
(C++14)
(C++11)
(C++11)
(C++11)
(C++17)
Common vocabulary types
(C++11)
(C++17)
(C++17)
(C++17)
(C++11)
(C++17)
(C++23)
Elementary string conversions
(C++17)
(C++17)

 
Function objects
Function invocation
(C++17)(C++23)
Identity function object
(C++20)
Transparent operator wrappers
(C++14)
(C++14)
(C++14)
(C++14)  
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)

Old binders and adaptors
(until C++17*)
(until C++17*)
(until C++17*)
(until C++17*)  
(until C++17*)
(until C++17*)(until C++17*)(until C++17*)(until C++17*)
(until C++20*)
(until C++20*)
(until C++17*)(until C++17*)
(until C++17*)(until C++17*)

(until C++17*)
(until C++17*)(until C++17*)(until C++17*)(until C++17*)
(until C++20*)
(until C++20*)
 
 
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 >
function& operator=( F&& f );
(4) (since C++11)
template< class F >
function& operator=( std::reference_wrapper<F> f ) noexcept;
(5) (since C++11)

Assigns a new target to std::function.

1) Assigns a copy of target of other, as if by executing function(other).swap(*this);
2) Moves the target of other to *this. other is in a valid state with an unspecified value.
3) Drops the current target. *this is empty after the call.
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) Sets the target of *this to a copy of f, as if by executing function(f).swap(*this);

Contents

[edit] 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.

[edit] Return value

*this

[edit] 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).

[edit] Defect reports

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

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

[edit] See also

(C++23)
replaces or destroys the target
(public member function of std::move_only_function) [edit]
(removed in C++17)
assigns a new target
(public member function) [edit]