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

22 General utilities library [utilities]

22.10 Function objects [function.objects]

22.10.17 Polymorphic function wrappers [func.wrap]

22.10.17.5 Copyable wrapper [func.wrap.copy]

22.10.17.5.1 General [func.wrap.copy.general]

The header provides partial specializations of copyable_functionfor each combination of the possible replacements of the placeholders cv, ref, and noex where

For each of the possible combinations of the placeholders mentioned above, there is a placeholder inv-quals defined as follows:

22.10.17.5.2 Class template copyable_function [func.wrap.copy.class]

namespace std { template<class R, class... ArgTypes> class copyable_function<R(ArgTypes...) cv _ref_ noexcept(_noex_)> { public: using result_type = R; copyable_function() noexcept; copyable_function(nullptr_t) noexcept; copyable_function(const copyable_function&); copyable_function(copyable_function&&) noexcept;template<class F> copyable_function(F&&);template<class T, class... Args> explicit copyable_function(in_place_type_t<T>, Args&&...);template<class T, class U, class... Args> explicit copyable_function(in_place_type_t<T>, initializer_list<U>, Args&&...); copyable_function& operator=(const copyable_function&); copyable_function& operator=(copyable_function&&); copyable_function& operator=(nullptr_t) noexcept;template<class F> copyable_function& operator=(F&&);~copyable_function();explicit operator bool() const noexcept; R operator()(ArgTypes...) cv ref noexcept(noex);void swap(copyable_function&) noexcept;friend void swap(copyable_function&, copyable_function&) noexcept;friend bool operator==(const copyable_function&, nullptr_t) noexcept;private: template<class VT> static constexpr bool is-callable-from = see below; };}

The copyable_function class template provides polymorphic wrappers that generalize the notion of a callable object ([func.def]).

These wrappers can store, copy, move, and call arbitrary callable objects, given a call signature.

Recommended practice: Implementations should avoid the use of dynamically allocated memory for a small contained value.

[Note 1:

Such small-object optimization can only be applied to a type Tfor which is_nothrow_move_constructible_v<T> is true.

— _end note_]

22.10.17.5.3 Constructors, assignments, and destructors [func.wrap.copy.ctor]

template<class VT> static constexpr bool _is-callable-from_ = _see below_;

If noex is true,is-callable-from<VT> is equal to:is_nothrow_invocable_r_v<R, VT cv _ref_, ArgTypes...> &&is_nothrow_invocable_r_v<R, VT _inv-quals_, ArgTypes...>

Otherwise, is-callable-from<VT> is equal to:is_invocable_r_v<R, VT cv _ref_, ArgTypes...> &&is_invocable_r_v<R, VT _inv-quals_, ArgTypes...>

copyable_function() noexcept; copyable_function(nullptr_t) noexcept;

Postconditions: *this has no target object.

copyable_function(const copyable_function& f);

Postconditions: *this has no target object if f had no target object.

Otherwise, the target object of *thisis a copy of the target object of f.

Throws: Any exception thrown by the initialization of the target object.

May throw bad_alloc.

copyable_function(copyable_function&& f) noexcept;

Postconditions: The target object of *this is the target object f had before construction, andf is in a valid state with an unspecified value.

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

Constraints:

Mandates:

Postconditions: *this has no target object if any of the following hold:

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

Throws: Any exception thrown by the initialization of the target object.

May throw bad_alloc unless VT is a function pointer or a specialization of reference_wrapper.

template<class T, class... Args> explicit copyable_function(in_place_type_t<T>, Args&&... args);

Constraints:

Mandates:

Postconditions: *this has a target object of type VTdirect-non-list-initialized with std​::​forward<Args>​(args)....

Throws: Any exception thrown by the initialization of the target object.

May throw bad_alloc unless VT is a pointer or a specialization of reference_wrapper.

template<class T, class U, class... Args> explicit copyable_function(in_place_type_t<T>, initializer_list<U> ilist, Args&&... args);

Constraints:

Mandates:

Postconditions: *this has a target object of type VTdirect-non-list-initialized withilist, std​::​forward<Args>(args)....

Throws: Any exception thrown by the initialization of the target object.

May throw bad_alloc unless VT is a pointer or a specialization of reference_wrapper.

copyable_function& operator=(const copyable_function& f);

Effects: Equivalent to: copyable_function(f).swap(*this);

copyable_function& operator=(copyable_function&& f);

Effects: Equivalent to: copyable_function(std​::​move(f)).swap(*this);

copyable_function& operator=(nullptr_t) noexcept;

Effects: Destroys the target object of *this, if any.

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

Effects: Equivalent to: copyable_function(std​::​forward<F>(f)).swap(*this);

Effects: Destroys the target object of *this, if any.

22.10.17.5.4 Invocation [func.wrap.copy.inv]

explicit operator bool() const noexcept;

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

R operator()(ArgTypes... args) cv _ref_ noexcept(_noex_);

Preconditions: *this has a target object.

Effects: Equivalent to:return INVOKE<R>(static_cast<F _inv-quals_>(f), std::forward<ArgTypes>(args)...);where f is an lvalue designating the target object of *this andF is the type of f.

22.10.17.5.5 Utility [func.wrap.copy.util]

void swap(copyable_function& other) noexcept;

Effects: Exchanges the target objects of *this and other.

friend void swap(copyable_function& f1, copyable_function& f2) noexcept;

Effects: Equivalent to f1.swap(f2).

friend bool operator==(const copyable_function& f, nullptr_t) noexcept;

Returns: true if f has no target object, otherwise false.