20 General utilities library [utilities] (original) (raw)

20.14.17.1 General [func.wrap.general]

Subclause [func.wrap] describes a polymorphic wrapper class that encapsulates arbitrary callable objects.

20.14.17.2 Class bad_­function_­call [func.wrap.badcall]

An exception of type bad_­function_­call is thrown byfunction​::​operator() ([func.wrap.func.inv]) when the function wrapper object has no target.

namespace std { class bad_function_call : public exception { public: const char* what() const noexcept override;};}

const char* what() const noexcept override;

Returns: Animplementation-defined ntbs.

20.14.17.3 Class template function [func.wrap.func]

20.14.17.3.1 General [func.wrap.func.general]

namespace std { template<class> class function; template<class R, class... ArgTypes> class function<R(ArgTypes...)> { public: using result_type = R; function() noexcept; function(nullptr_t) noexcept; function(const function&); function(function&&) noexcept;template<class F> function(F); function& operator=(const function&); function& operator=(function&&); function& operator=(nullptr_t) noexcept;template<class F> function& operator=(F&&);template<class F> function& operator=(reference_wrapper<F>) noexcept;~function();void swap(function&) noexcept;explicit operator bool() const noexcept; R operator()(ArgTypes...) const;const type_info& target_type() const noexcept;template<class T> T* target() noexcept;template<class T> const T* target() const noexcept;};template<class R, class... ArgTypes> function(R(*)(ArgTypes...)) -> function<R(ArgTypes...)>;template<class F> function(F) -> function<_see below_>;template<class R, class... ArgTypes> bool operator==(const function<R(ArgTypes...)>&, nullptr_t) noexcept;template<class R, class... ArgTypes> void swap(function<R(ArgTypes...)>&, function<R(ArgTypes...)>&) noexcept;}

The function class template provides polymorphic wrappers that generalize the notion of a function pointer.

Wrappers can store, copy, and call arbitrary callable objects, given acall signature, allowing functions to be first-class objects.

[Note 1:

The types deduced by the deduction guides for functionmight change in future revisions of C++.

— _end note_]

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

function(nullptr_t) noexcept;

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_­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: F is Lvalue-Callable ([func.wrap.func]) for argument typesArgTypes... and return type R.

Preconditions: F meets the Cpp17CopyConstructible requirements.

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

Otherwise, *this targets a copy of finitialized with std​::​move(f).

Throws: Nothing if f is a specialization of reference_­wrapper or a function pointer.

Otherwise, may throw bad_­allocor any exception thrown by F's copy or move constructor.

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

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

Constraints: &F​::​operator() is well-formed when treated as an unevaluated operand anddecltype(​&F​::​operator()) is of the formR(G​::​*)(A...) cv & noexceptfor a class type G.

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: 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);

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.

20.14.17.3.3 Modifiers [func.wrap.func.mod]

void swap(function& other) noexcept;

Effects: Interchanges the targets of *this and other.

20.14.17.3.4 Capacity [func.wrap.func.cap]

explicit operator bool() const noexcept;

Returns: true if *this has a target, otherwise false.

20.14.17.3.5 Invocation [func.wrap.func.inv]

R operator()(ArgTypes... args) const;

Throws: bad_­function_­call if !*this; otherwise, any exception thrown by the wrapped callable object.

20.14.17.3.6 Target access [func.wrap.func.targ]

const type_info& target_type() const noexcept;

Returns: If *this has a target of type T,typeid(T); otherwise, typeid(void).

template<class T> T* target() noexcept;template<class T> const T* target() const noexcept;

Returns: If target_­type() == typeid(T)a pointer to the stored function target; otherwise a null pointer.

20.14.17.3.7 Null pointer comparison operator functions [func.wrap.func.nullptr]

template<class R, class... ArgTypes> bool operator==(const function<R(ArgTypes...)>& f, nullptr_t) noexcept;

20.14.17.3.8 Specialized algorithms [func.wrap.func.alg]

template<class R, class... ArgTypes> void swap(function<R(ArgTypes...)>& f1, function<R(ArgTypes...)>& f2) noexcept;

Effects: As if by: f1.swap(f2);