Files
2025-10-25 03:02:53 +03:00

16 KiB
Raw Permalink Blame History

[func.wrap.copy]

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]

1

#

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

cv is either const or empty,

ref is either &, &&, or empty, and

noex is either true or false.

2

#

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

If ref is empty, let inv-quals be cv&,

otherwise, let inv-quals be cv ref.

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; // [func.wrap.copy.ctor], constructors, assignments, and destructor copyable_function() noexcept; copyable_function(nullptr_t) noexcept; copyable_function(const copyable_function&); copyable_function(copyable_function&&) noexcept; template copyable_function(F&&); template<class T, class... Args>explicit copyable_function(in_place_type_t, Args&&...); template<class T, class U, class... Args>explicit copyable_function(in_place_type_t, initializer_list, Args&&...);

copyable_function& operator=(const copyable_function&); copyable_function& operator=(copyable_function&&); copyable_function& operator=(nullptr_t) noexcept; template copyable_function& operator=(F&&); ~copyable_function(); // [func.wrap.copy.inv], invocationexplicit operator bool() const noexcept; R operator()(ArgTypes...) cv ref noexcept(noex); // [func.wrap.copy.util], utilityvoid swap(copyable_function&) noexcept; friend void swap(copyable_function&, copyable_function&) noexcept; friend bool operator==(const copyable_function&, nullptr_t) noexcept; private:templatestatic constexpr bool is-callable-from = see below; // exposition only};}

1

#

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.

2

#

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 T for which is_nothrow_move_constructible_v is true.

— end note]

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

🔗

template<class VT> static constexpr bool is-callable-from = see below;

1

#

If noex is true,is-callable-from 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 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;

2

#

Postconditions: *this has no target object.

🔗

copyable_function(const copyable_function& f);

3

#

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

Otherwise, the target object of *this is a copy of the target object of f.

4

#

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

May throw bad_alloc.

🔗

copyable_function(copyable_function&& f) noexcept;

5

#

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

6

#

Let VT be decay_t.

7

#

Constraints:

remove_cvref_t is not the same type as copyable_function, and

remove_cvref_t is not a specialization of in_place_type_t, and

is-callable-from is true.

8

#

Mandates:

is_constructible_v<VT, F> is true, and

is_copy_constructible_v is true.

9

#

Preconditions: VT meets the Cpp17Destructible andCpp17CopyConstructible requirements.

10

#

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

f is a null function pointer value, or

f is a null member pointer value, or

remove_cvref_t is a specialization of the copyable_function class template, and f has no target object.

Otherwise, *this has a target object of type VT direct-non-list-initialized with std::forward(f).

11

#

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

12

#

Let VT be decay_t.

13

#

Constraints:

is_constructible_v<VT, Args...> is true, and

is-callable-from is true.

14

#

Mandates:

VT is the same type as T, and

is_copy_constructible_v is true.

15

#

Preconditions: VT meets the Cpp17Destructible andCpp17CopyConstructible requirements.

16

#

Postconditions: *this has a target object of type VT direct-non-list-initialized with std::forward(args)....

17

#

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

18

#

Let VT be decay_t.

19

#

Constraints:

is_constructible_v<VT, initializer_list&, Args...> istrue, and

is-callable-from is true.

20

#

Mandates:

VT is the same type as T, and

is_copy_constructible_v is true.

21

#

Preconditions: VT meets the Cpp17Destructible andCpp17CopyConstructible requirements.

22

#

Postconditions: *this has a target object of type VT direct-non-list-initialized withilist, std::forward(args)....

23

#

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

24

#

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

25

#

Returns: *this.

🔗

copyable_function& operator=(copyable_function&& f);

26

#

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

27

#

Returns: *this.

🔗

copyable_function& operator=(nullptr_t) noexcept;

28

#

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

29

#

Returns: *this.

🔗

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

30

#

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

31

#

Returns: *this.

🔗

~copyable_function();

32

#

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

22.10.17.5.4 Invocation [func.wrap.copy.inv]

🔗

explicit operator bool() const noexcept;

1

#

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

🔗

R operator()(ArgTypes... args) cv ref noexcept(noex);

2

#

Preconditions: *this has a target object.

3

#

Effects: Equivalent to:return INVOKE(static_cast<F inv-quals>(f), std::forward(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;

1

#

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

🔗

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

2

#

Effects: Equivalent to f1.swap(f2).

🔗

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

3

#

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