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

15 KiB
Raw Permalink Blame History

[func.wrap.func]

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.1 General [func.wrap.func.general]

🔗

namespace std {template<class R, class... ArgTypes>class function<R(ArgTypes...)> {public:using result_type = R; // [func.wrap.func.con], construct/copy/destroy function() noexcept; function(nullptr_t) noexcept; function(const function&); function(function&&) noexcept; template function(F&&);

function& operator=(const function&); function& operator=(function&&); function& operator=(nullptr_t) noexcept; template function& operator=(F&&); template function& operator=(reference_wrapper) noexcept; ~function(); // [func.wrap.func.mod], function modifiersvoid swap(function&) noexcept; // [func.wrap.func.cap], function capacityexplicit operator bool() const noexcept; // [func.wrap.func.inv], function invocation R operator()(ArgTypes...) const; // [func.wrap.func.targ], function target accessconst type_info& target_type() const noexcept; template T* target() noexcept; template const T* target() const noexcept; }; template<class R, class... ArgTypes> function(R(*)(ArgTypes...)) -> function<R(ArgTypes...)>; template function(F) -> function<see below>;}

1

#

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

Wrappers can store, copy, and call arbitrary callable objects ([func.def]), given a call signature ([func.def]).

2

#

The function class template is a call wrapper ([func.def]) whose call signature ([func.def]) is R(ArgTypes...).

3

#

[Note 1:

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

— end note]

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

🔗

function() noexcept;

1

#

Postconditions: !*this.

🔗

function(nullptr_t) noexcept;

2

#

Postconditions: !*this.

🔗

function(const function& f);

3

#

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

4

#

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.

5

#

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;

6

#

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.

7

#

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

8

#

Let FD be decay_t.

9

#

Constraints:

is_same_v<remove_cvref_t, function> is false, and

is_invocable_r_v<R, FD&, ArgTypes...> is true.

10

#

Mandates:

is_copy_constructible_v is true, and

is_constructible_v<FD, F> is true.

11

#

Preconditions: FD meets the Cpp17CopyConstructible requirements.

12

#

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

  • (12.1)

    f is a null function pointer value.

  • (12.2)

    f is a null member pointer value.

  • (12.3)

    remove_cvref_t is a specialization of the function class template, and!f is true.

13

#

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

14

#

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.

15

#

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

16

#

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

F::operator() is a non-static member function anddecltype(&F::operator()) is either of the formR(G::*)(A...) cv &opt noexceptopt or of the formR(*)(G, A...) noexceptopt for a type G, or

F::operator() is a static member function anddecltype(&F::operator()) is of the formR(*)(A...) noexceptopt.

17

#

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

18

#

[Example 1: void f() {int i{5}; function g = & { return i; }; // deduces function<int(double)>} — end example]

🔗

function& operator=(const function& f);

19

#

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

20

#

Returns: *this.

🔗

function& operator=(function&& f);

21

#

Effects: Replaces the target of *this with the target of f.

22

#

Returns: *this.

🔗

function& operator=(nullptr_t) noexcept;

23

#

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

24

#

Postconditions: !(*this).

25

#

Returns: *this.

🔗

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

26

#

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

27

#

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

28

#

Returns: *this.

🔗

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

29

#

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

30

#

Returns: *this.

🔗

~function();

31

#

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

22.10.17.3.3 Modifiers [func.wrap.func.mod]

🔗

void swap(function& other) noexcept;

1

#

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

22.10.17.3.4 Capacity [func.wrap.func.cap]

🔗

explicit operator bool() const noexcept;

1

#

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

22.10.17.3.5 Invocation [func.wrap.func.inv]

🔗

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

1

#

Returns: INVOKE(f, std::forward(args)...) ([func.require]), where f is the target object ([func.def]) of *this.

2

#

Throws: bad_function_call if !*this; otherwise, any exception thrown by the target object.

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

🔗

const type_info& target_type() const noexcept;

1

#

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;

2

#

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

22.10.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;

1

#

Returns: !f.

22.10.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;

1

#

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