20 General utilities library [utilities]

20.14 Function objects [function.objects]

20.14.16 Polymorphic function wrappers [func.wrap]

20.14.16.2 Class template function [func.wrap.func]

20.14.16.2.1 Constructors and destructor [func.wrap.func.con]

function() noexcept;
Postconditions: !*this.
function(nullptr_t) noexcept;
Postconditions: !*this.
function(const function& f);
Postconditions: !*this if !f; otherwise, *this targets a copy of f.target().
Throws: Nothing if f's target is a specialization of reference_­wrapper or a function pointer.
Otherwise, may throw bad_­alloc or any exception thrown by the copy constructor of the stored callable object.
[Note
:
Implementations should avoid the use of dynamically allocated memory for small callable objects, for example, where f's target is an object holding only a pointer or reference to an object and a member function pointer.
— end note
]
function(function&& f) noexcept;
Postconditions: If !f, *this has no target; otherwise, the target of *this is equivalent to the target of f before the construction, and f is in a valid state with an unspecified value.
[Note
:
Implementations should avoid the use of dynamically allocated memory for small callable objects, for example, where f's target is an object holding only a pointer or reference to an object and a member function pointer.
— end note
]
template<class F> function(F f);
Constraints: F is Lvalue-Callable ([func.wrap.func]) for argument types ArgTypes... and return type R.
Preconditions: F meets the Cpp17CopyConstructible requirements.
Postconditions: !*this if any of the following hold:
  • f is a null function pointer value.
  • f is a null member pointer value.
  • F is an instance of the function class template, and !f.
Otherwise, *this targets a copy of f initialized with std​::​move(f).
[Note
:
Implementations should avoid the use of dynamically allocated memory for small callable objects, for example, where f is an object holding only a pointer or reference to an object and a member function pointer.
— end note
]
Throws: Nothing if f is a specialization of reference_­wrapper or a function pointer.
Otherwise, may throw bad_­alloc or any exception thrown by F's copy or move constructor.
template<class F> function(F) -> function<see below>;
Constraints: &F​::​operator() is well-formed when treated as an unevaluated operand and decltype(&F​::​operator()) is of the form R(G​::​*)(A...) cv & noexcept for a class type G.
Remarks: The deduced type is function<R(A...)>.
[Example
:
void f() {
  int i{5};
  function g = [&](double) { return i; };       // deduces function<int(double)>
}
— end example
]
function& operator=(const function& f);
Effects: As if by function(f).swap(*this);
Returns: *this.
function& operator=(function&& f);
Effects: Replaces the target of *this with the target of f.
Returns: *this.
function& operator=(nullptr_t) noexcept;
Effects: If *this != nullptr, destroys the target of this.
Postconditions: !(*this).
Returns: *this.
template<class F> function& operator=(F&& f);
Constraints: decay_­t<F> is Lvalue-Callable ([func.wrap.func]) for argument types ArgTypes... and return type R.
Effects: As if by: function(std​::​forward<F>(f)).swap(*this);
Returns: *this.
template<class F> function& operator=(reference_wrapper<F> f) noexcept;
Effects: As if by: function(f).swap(*this);
Returns: *this.
~function();
Effects: If *this != nullptr, destroys the target of this.