[func.wrap.func.con] (original) (raw)

22 General utilities library [utilities]

22.10 Function objects [function.objects]

22.10.17 Polymorphic function wrappers [func.wrap]

22.10.17.3 Class template function [func.wrap.func]

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

function(nullptr_t) noexcept;

function(const function& f);

Postconditions: !*this if !f; otherwise, the target object of *this is a copy of the target object of f.

Throws: Nothing if f's target is a specialization of reference_wrapper or a function pointer.

Otherwise, may throw bad_allocor any exception thrown by the copy constructor of the stored callable object.

Recommended practice: Implementations should avoid the use of dynamically allocated memory for small callable objects, for example, wheref's target is an object holding only a pointer or reference to an object and a member function pointer.

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, andf is in a valid state with an unspecified value.

Recommended practice: 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.

template<class F> function(F&& f);

Constraints:

Mandates:

Postconditions: !*this is true if any of the following hold:

Otherwise, *this has a target object of type FDdirect-non-list-initialized with std​::​forward<F>(f).

Throws: Nothing if FD is a specialization of reference_wrapper or a function pointer type.

Otherwise, may throw bad_alloc or any exception thrown by the initialization of the target object.

Recommended practice: Implementations should avoid the use of dynamically allocated memory for small callable objects, for example, where f refers to an object holding only a pointer or reference to an object and a member function pointer.

template<class F> function(F) -> function<_see below_>;

Constraints: &F​::​operator() is well-formed when treated as an unevaluated operand and either

Remarks: The deduced type is function<R(A...)>.

[Example 1: void f() { int i{5}; function g = [&](double) { return i; }; } — _end example_]

function& operator=(const function& f);

Effects: As if by function(f).swap(*this);

function& operator=(function&& f);

Effects: Replaces the target of *thiswith the target of f.

function& operator=(nullptr_t) noexcept;

Effects: If *this != nullptr, destroys the target of this.

Postconditions: !(*this).

template<class F> function& operator=(F&& f);

Constraints: is_invocable_r_v<R, decay_t<F>&, ArgTypes...> is true.

Effects: As if by: function(std​::​forward<F>(f)).swap(*this);

template<class F> function& operator=(reference_wrapper<F> f) noexcept;

Effects: As if by: function(f).swap(*this);

Effects: If *this != nullptr, destroys the target of this.