[func.wrap.func.con]

# 22 General utilities library [[utilities]](./#utilities)

## 22.10 Function objects [[function.objects]](function.objects#func.wrap.func.con)

### 22.10.17 Polymorphic function wrappers [[func.wrap]](func.wrap#func.con)

#### 22.10.17.3 Class template function [[func.wrap.func]](func.wrap.func#con)

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

[ð](#lib:function,constructor)

`function() noexcept;
`

[1](#1)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L13726)

*Postconditions*: !*this[.](#1.sentence-1)

[ð](#lib:function,constructor_)

`function(nullptr_t) noexcept;
`

[2](#2)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L13737)

*Postconditions*: !*this[.](#2.sentence-1)

[ð](#lib:function,constructor__)

`function(const function& f);
`

[3](#3)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L13748)

*Postconditions*: !*this if !f; otherwise,
the target object of *this is a copy of the target object of f[.](#3.sentence-1)

[4](#4)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L13753)

*Throws*: Nothing if f's target is
a specialization of reference_wrapper or
a function pointer[.](#4.sentence-1)

Otherwise, may throw bad_alloc or any exception thrown by the copy constructor of the stored callable object[.](#4.sentence-2)

[5](#5)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L13760)

*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[.](#5.sentence-1)

[ð](#lib:function,constructor___)

`function(function&& f) noexcept;
`

[6](#6)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L13774)

*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[.](#6.sentence-1)

[7](#7)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L13781)

*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[.](#7.sentence-1)

[ð](#lib:function,constructor____)

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

[8](#8)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L13795)

Let FD be decay_t<F>[.](#8.sentence-1)

[9](#9)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L13798)

*Constraints*: 

- [(9.1)](#9.1)

is_same_v<remove_cvref_t<F>, function> is false, and

- [(9.2)](#9.2)

is_invocable_r_v<R, FD&, ArgTypes...> is true[.](#9.sentence-1)

[10](#10)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L13807)

*Mandates*: 

- [(10.1)](#10.1)

is_copy_constructible_v<FD> is true, and

- [(10.2)](#10.2)

is_constructible_v<FD, F> is true[.](#10.sentence-1)

[11](#11)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L13816)

*Preconditions*: FD meets the [*Cpp17CopyConstructible*](utility.arg.requirements#:Cpp17CopyConstructible "16.4.4.2 Template argument requirements [utility.arg.requirements]") requirements[.](#11.sentence-1)

[12](#12)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L13820)

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

- [(12.1)](#12.1)

  f is a null function pointer value[.](#12.1.sentence-1)

- [(12.2)](#12.2)

  f is a null member pointer value[.](#12.2.sentence-1)

- [(12.3)](#12.3)

  remove_cvref_t<F> is
a specialization of the function class template, and!f is true[.](#12.3.sentence-1)

[13](#13)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L13831)

Otherwise, *this has a target object of type FD direct-non-list-initialized with std​::​forward<F>(f)[.](#13.sentence-1)

[14](#14)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L13835)

*Throws*: Nothing if FD is
a specialization of reference_wrapper or
a function pointer type[.](#14.sentence-1)

Otherwise, may throw bad_alloc or
any exception thrown by the initialization of the target object[.](#14.sentence-2)

[15](#15)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L13843)

*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[.](#15.sentence-1)

[ð](#itemdecl:6)

`template<class F> function(F) -> function<see below>;
`

[16](#16)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L13857)

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

- [(16.1)](#16.1)

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

- [(16.2)](#16.2)

F​::​operator() is a static member function anddecltype(&F​::​operator()) is of the formR(*)(A...) noexceptopt[.](#16.sentence-1)

[17](#17)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L13874)

*Remarks*: The deduced type is function<R(A...)>[.](#17.sentence-1)

[18](#18)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L13878)

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

[ð](#lib:operator=,function)

`function& operator=(const function& f);
`

[19](#19)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L13895)

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

[20](#20)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L13899)

*Returns*: *this[.](#20.sentence-1)

[ð](#lib:operator=,function_)

`function& operator=(function&& f);
`

[21](#21)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L13910)

*Effects*: Replaces the target of *this with the target of f[.](#21.sentence-1)

[22](#22)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L13915)

*Returns*: *this[.](#22.sentence-1)

[ð](#lib:operator=,function__)

`function& operator=(nullptr_t) noexcept;
`

[23](#23)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L13926)

*Effects*: If *this != nullptr, destroys the target of this[.](#23.sentence-1)

[24](#24)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L13930)

*Postconditions*: !(*this)[.](#24.sentence-1)

[25](#25)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L13934)

*Returns*: *this[.](#25.sentence-1)

[ð](#lib:operator=,function___)

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

[26](#26)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L13945)

*Constraints*: is_invocable_r_v<R, decay_t<F>&, ArgTypes...> is true[.](#26.sentence-1)

[27](#27)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L13949)

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

[28](#28)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L13953)

*Returns*: *this[.](#28.sentence-1)

[ð](#lib:operator=,function____)

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

[29](#29)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L13964)

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

[30](#30)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L13968)

*Returns*: *this[.](#30.sentence-1)

[ð](#lib:function,destructor)

`~function();
`

[31](#31)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L13979)

*Effects*: If *this != nullptr, destroys the target of this[.](#31.sentence-1)
