[func.wrap.move]

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

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

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

#### 22.10.17.4 Move-only wrapper [func.wrap.move]

#### [22.10.17.4.1](#general) General [[func.wrap.move.general]](func.wrap.move.general)

[1](#general-1)

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

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

- [(1.1)](#general-1.1)

cv is either const or empty,

- [(1.2)](#general-1.2)

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

- [(1.3)](#general-1.3)

*noex* is either true or false[.](#general-1.sentence-1)

[2](#general-2)

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

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

- [(2.1)](#general-2.1)

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

- [(2.2)](#general-2.2)

otherwise, let *inv-quals* be cv *ref*[.](#general-2.sentence-1)

#### [22.10.17.4.2](#class) Class template move_only_function [[func.wrap.move.class]](func.wrap.move.class)

[ð](#lib:move_only_function)

namespace std {template<class R, class... ArgTypes>class move_only_function<R(ArgTypes...) cv *ref* noexcept(*noex*)> {public:using result_type = R; // [[func.wrap.move.ctor]](#ctor "22.10.17.4.3 Constructors, assignments, and destructor"), constructors, assignments, and destructor move_only_function() noexcept;
 move_only_function(nullptr_t) noexcept;
 move_only_function(move_only_function&&) noexcept; template<class F> move_only_function(F&&); template<class T, class... Args>explicit move_only_function(in_place_type_t<T>, Args&&...); template<class T, class U, class... Args>explicit move_only_function(in_place_type_t<T>, initializer_list<U>, Args&&...);

 move_only_function& operator=(move_only_function&&);
 move_only_function& operator=(nullptr_t) noexcept; template<class F> move_only_function& operator=(F&&); ~move_only_function(); // [[func.wrap.move.inv]](#inv "22.10.17.4.4 Invocation"), invocationexplicit operator bool() const noexcept;
 R operator()(ArgTypes...) cv *ref* noexcept(*noex*); // [[func.wrap.move.util]](#util "22.10.17.4.5 Utility"), utilityvoid swap(move_only_function&) noexcept; friend void swap(move_only_function&, move_only_function&) noexcept; friend bool operator==(const move_only_function&, nullptr_t) noexcept; private:template<class VT>static constexpr bool *is-callable-from* = *see below*; // *exposition only*};}

[1](#class-1)

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

The move_only_function class template provides polymorphic wrappers
that generalize the notion of a callable object ([[func.def]](func.def "22.10.3 Definitions"))[.](#class-1.sentence-1)

These wrappers can store, move, and call arbitrary callable objects,
given a call signature[.](#class-1.sentence-2)

[2](#class-2)

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

*Recommended practice*: Implementations should avoid the use of dynamically allocated memory
for a small contained value[.](#class-2.sentence-1)

[*Note [1](#class-note-1)*:

Such small-object optimization can only be applied to a type T for which is_nothrow_move_constructible_v<T> is true[.](#class-2.sentence-2)

â *end note*]

#### [22.10.17.4.3](#ctor) Constructors, assignments, and destructor [[func.wrap.move.ctor]](func.wrap.move.ctor)

[ð](#:move_only_function::is-callable-from)

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

[1](#ctor-1)

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

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

[ð](#lib:move_only_function,constructor)

`move_only_function() noexcept;
move_only_function(nullptr_t) noexcept;
`

[2](#ctor-2)

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

*Postconditions*: *this has no target object[.](#ctor-2.sentence-1)

[ð](#lib:move_only_function,constructor_)

`move_only_function(move_only_function&& f) noexcept;
`

[3](#ctor-3)

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

*Postconditions*: The target object of *this is
the target object f had before construction, andf is in a valid state with an unspecified value[.](#ctor-3.sentence-1)

[ð](#lib:move_only_function,constructor__)

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

[4](#ctor-4)

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

Let VT be decay_t<F>[.](#ctor-4.sentence-1)

[5](#ctor-5)

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

*Constraints*: 

- [(5.1)](#ctor-5.1)

remove_cvref_t<F> is not the same type as move_only_function, and

- [(5.2)](#ctor-5.2)

remove_cvref_t<F> is not a specialization of in_place_type_t, and

- [(5.3)](#ctor-5.3)

*is-callable-from*<VT> is true[.](#ctor-5.sentence-1)

[6](#ctor-6)

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

*Mandates*: is_constructible_v<VT, F> is true[.](#ctor-6.sentence-1)

[7](#ctor-7)

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

*Preconditions*: VT meets the [*Cpp17Destructible*](utility.arg.requirements#:Cpp17Destructible "16.4.4.2 Template argument requirements [utility.arg.requirements]") requirements, and
if is_move_constructible_v<VT> is true,VT meets the [*Cpp17MoveConstructible*](utility.arg.requirements#:Cpp17MoveConstructible "16.4.4.2 Template argument requirements [utility.arg.requirements]") requirements[.](#ctor-7.sentence-1)

[8](#ctor-8)

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

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

- [(8.1)](#ctor-8.1)

f is a null function pointer value, or

- [(8.2)](#ctor-8.2)

f is a null member pointer value, or

- [(8.3)](#ctor-8.3)

remove_cvref_t<F> is a specialization of
the move_only_function class template,
and f has no target object[.](#ctor-8.sentence-1)

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

[9](#ctor-9)

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

*Throws*: Any exception thrown by the initialization of the target object[.](#ctor-9.sentence-1)

May throw bad_alloc unless VT is
a function pointer or a specialization of reference_wrapper[.](#ctor-9.sentence-2)

[ð](#lib:move_only_function,constructor___)

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

[10](#ctor-10)

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

Let VT be decay_t<T>[.](#ctor-10.sentence-1)

[11](#ctor-11)

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

*Constraints*: 

- [(11.1)](#ctor-11.1)

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

- [(11.2)](#ctor-11.2)

*is-callable-from*<VT> is true[.](#ctor-11.sentence-1)

[12](#ctor-12)

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

*Mandates*: VT is the same type as T[.](#ctor-12.sentence-1)

[13](#ctor-13)

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

*Preconditions*: VT meets the [*Cpp17Destructible*](utility.arg.requirements#:Cpp17Destructible "16.4.4.2 Template argument requirements [utility.arg.requirements]") requirements, and
if is_move_constructible_v<VT> is true,VT meets the [*Cpp17MoveConstructible*](utility.arg.requirements#:Cpp17MoveConstructible "16.4.4.2 Template argument requirements [utility.arg.requirements]") requirements[.](#ctor-13.sentence-1)

[14](#ctor-14)

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

*Postconditions*: *this has a target object of type VT direct-non-list-initialized with std​::​forward<Args>​(args)...[.](#ctor-14.sentence-1)

[15](#ctor-15)

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

*Throws*: Any exception thrown by the initialization of the target object[.](#ctor-15.sentence-1)

May throw bad_alloc unless VT is
a function pointer or a specialization of reference_wrapper[.](#ctor-15.sentence-2)

[ð](#lib:move_only_function,constructor____)

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

[16](#ctor-16)

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

Let VT be decay_t<T>[.](#ctor-16.sentence-1)

[17](#ctor-17)

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

*Constraints*: 

- [(17.1)](#ctor-17.1)

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

- [(17.2)](#ctor-17.2)

*is-callable-from*<VT> is true[.](#ctor-17.sentence-1)

[18](#ctor-18)

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

*Mandates*: VT is the same type as T[.](#ctor-18.sentence-1)

[19](#ctor-19)

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

*Preconditions*: VT meets the [*Cpp17Destructible*](utility.arg.requirements#:Cpp17Destructible "16.4.4.2 Template argument requirements [utility.arg.requirements]") requirements, and
if is_move_constructible_v<VT> is true,VT meets the [*Cpp17MoveConstructible*](utility.arg.requirements#:Cpp17MoveConstructible "16.4.4.2 Template argument requirements [utility.arg.requirements]") requirements[.](#ctor-19.sentence-1)

[20](#ctor-20)

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

*Postconditions*: *this has a target object of type VT direct-non-list-initialized withilist, std​::​forward<Args>(args)...[.](#ctor-20.sentence-1)

[21](#ctor-21)

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

*Throws*: Any exception thrown by the initialization of the target object[.](#ctor-21.sentence-1)

May throw bad_alloc unless VT is
a function pointer or a specialization of reference_wrapper[.](#ctor-21.sentence-2)

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

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

[22](#ctor-22)

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

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

[23](#ctor-23)

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

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

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

`move_only_function& operator=(nullptr_t) noexcept;
`

[24](#ctor-24)

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

*Effects*: Destroys the target object of *this, if any[.](#ctor-24.sentence-1)

[25](#ctor-25)

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

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

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

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

[26](#ctor-26)

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

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

[27](#ctor-27)

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

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

[ð](#lib:move_only_function,destructor)

`~move_only_function();
`

[28](#ctor-28)

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

*Effects*: Destroys the target object of *this, if any[.](#ctor-28.sentence-1)

#### [22.10.17.4.4](#inv) Invocation [[func.wrap.move.inv]](func.wrap.move.inv)

[ð](#lib:operator_bool,move_only_function)

`explicit operator bool() const noexcept;
`

[1](#inv-1)

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

*Returns*: true if *this has a target object, otherwise false[.](#inv-1.sentence-1)

[ð](#lib:operator(),move_only_function)

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

[2](#inv-2)

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

*Preconditions*: *this has a target object[.](#inv-2.sentence-1)

[3](#inv-3)

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

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

#### [22.10.17.4.5](#util) Utility [[func.wrap.move.util]](func.wrap.move.util)

[ð](#lib:swap,move_only_function)

`void swap(move_only_function& other) noexcept;
`

[1](#util-1)

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

*Effects*: Exchanges the target objects of *this and other[.](#util-1.sentence-1)

[ð](#lib:swap,move_only_function_)

`friend void swap(move_only_function& f1, move_only_function& f2) noexcept;
`

[2](#util-2)

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

*Effects*: Equivalent to f1.swap(f2)[.](#util-2.sentence-1)

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

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

[3](#util-3)

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

*Returns*: true if f has no target object, otherwise false[.](#util-3.sentence-1)
