[#absl-AnyInvocable]
= xref:absl.adoc[absl]::AnyInvocable
:relfileprefix: ../
:mrdocs:

absl&colon;&colon;AnyInvocable

== Synopsis

Declared in `&lt;absl&sol;functional&sol;any&lowbar;invocable&period;h&gt;`

[source,cpp,subs="verbatim,replacements,macros,-callouts"]
----
template&lt;class Sig&gt;
class AnyInvocable;
----

== Description

`absl&colon;&colon;AnyInvocable` is a functional wrapper type, like `std&colon;&colon;function`, that assumes ownership of an invocable object&period; Unlike `std&colon;&colon;function`, an `absl&colon;&colon;AnyInvocable` is more type&hyphen;safe and provides the following additional benefits&colon;

&ast; Properly adheres to const correctness of the underlying type &ast; Is move&hyphen;only so avoids concurrency problems with copied invocables and unnecessary copies in general&period; &ast; Supports reference qualifiers allowing it to perform unique actions (noted below)&period;

`absl&colon;&colon;AnyInvocable` is a template, and an `absl&colon;&colon;AnyInvocable` instantiation may wrap any invocable object with a compatible function signature, e&period;g&period; having arguments and return types convertible to types matching the `absl&colon;&colon;AnyInvocable` signature, and also matching any stated reference qualifiers, as long as that type is moveable&period; It therefore provides broad type erasure for functional objects&period;

An `absl&colon;&colon;AnyInvocable` is typically used as a type&hyphen;erased function parameter for accepting various functional objects&colon;

&sol;&sol; Define a function taking an AnyInvocable parameter&period; void my&lowbar;func(absl&colon;&colon;AnyInvocable&lt;int()&gt; f) &lcub; &period;&period;&period; &rcub;&semi;

&sol;&sol; That function can accept any invocable type&colon;

&sol;&sol; Accept a function reference&period; We don&apos;t need to move a reference&period; int func1() &lcub; return 0&semi; &rcub;&semi; my&lowbar;func(func1)&semi;

&sol;&sol; Accept a lambda&period; We use std&colon;&colon;move here because otherwise my&lowbar;func would &sol;&sol; copy the lambda&period; auto lambda &equals;  &lcub; return 0&semi; &rcub;&semi; my&lowbar;func(std&colon;&colon;move(lambda))&semi;

&sol;&sol; Accept a function pointer&period; We don&apos;t need to move a function pointer&period; func2 &equals; &amp;func1&semi; my&lowbar;func(func2)&semi;

&sol;&sol; Accept an std&colon;&colon;function by moving it&period; Note that the lambda is copyable &sol;&sol; (satisfying std&colon;&colon;function requirements) and moveable (satisfying &sol;&sol; absl&colon;&colon;AnyInvocable requirements)&period; std&colon;&colon;function&lt;int()&gt; func6 &equals;  &lcub; return 0&semi; &rcub;&semi; my&lowbar;func(std&colon;&colon;move(func6))&semi;

`AnyInvocable` also properly respects `const` qualifiers, reference qualifiers, and the `noexcept` specification as part of the user&hyphen;specified function type (e&period;g&period; `AnyInvocable&lt;void() const &amp;&amp; noexcept&gt;`)&period; These qualifiers will be applied to the `AnyInvocable` object&apos;s `operator()`, and the underlying invocable must be compatible with those qualifiers&period;

Comparison of const and non&hyphen;const function types&colon;

&sol;&sol; Store a closure inside of `func` with the function type `int()`&period; &sol;&sol; Note that we have made `func` itself `const`&period; const AnyInvocable&lt;int()&gt; func &equals; &lcub; return 0&semi; &rcub;&semi;

func()&semi; &sol;&sol; Compile&hyphen;error&colon; the passed type `int()` isn&apos;t `const`&period;

&sol;&sol; Store a closure inside of `const&lowbar;func` with the function type &sol;&sol; `int() const`&period; &sol;&sol; Note that we have also made `const&lowbar;func` itself `const`&period; const AnyInvocable&lt;int() const&gt; const&lowbar;func &equals; &lcub; return 0&semi; &rcub;&semi;

const&lowbar;func()&semi; &sol;&sol; Fine&colon; `int() const` is `const`&period;

In the above example, the call `func()` would have compiled if `std&colon;&colon;function` were used even though the types are not const compatible&period; This is a bug, and using `absl&colon;&colon;AnyInvocable` properly detects that bug&period;

In addition to affecting the signature of `operator()`, the `const` and reference qualifiers of the function type also appropriately constrain which kinds of invocable objects you are allowed to place into the `AnyInvocable` instance&period; If you specify a function type that is const&hyphen;qualified, then anything that you attempt to put into the `AnyInvocable` must be callable on a `const` instance of that type&period;

Constraint example&colon;

&sol;&sol; Fine because the lambda is callable when `const`&period; AnyInvocable&lt;int() const&gt; func &equals; [&equals;]&lcub; return 0&semi; &rcub;&semi;

&sol;&sol; This is a compile&hyphen;error because the lambda isn&apos;t callable when `const`&period; AnyInvocable&lt;int() const&gt; error &equals; [&equals;] mutable &lcub; return 0&semi; &rcub;&semi;

An `&amp;&amp;` qualifier can be used to express that an `absl&colon;&colon;AnyInvocable` instance should be invoked at most once&colon;

&sol;&sol; Invokes `continuation` with the logical result of an operation when &sol;&sol; that operation completes (common in asynchronous code)&period; void CallOnCompletion(AnyInvocable&lt;void(int)&amp;&amp;&gt; continuation) &lcub; int result&lowbar;of&lowbar;foo &equals; foo()&semi;

&sol;&sol; `std&colon;&colon;move` is required because the `operator()` of `continuation` is &sol;&sol; rvalue&hyphen;reference qualified&period; std&colon;&colon;move(continuation)(result&lowbar;of&lowbar;foo)&semi; &rcub;

Attempting to call `absl&colon;&colon;AnyInvocable` multiple times in such a case results in undefined behavior&period;

Invoking an empty `absl&colon;&colon;AnyInvocable` results in undefined behavior&colon;

&sol;&sol; Create an empty instance using the default constructor&period; AnyInvocable&lt;void()&gt; empty&semi; empty()&semi; &sol;&sol; WARNING&colon; Undefined behavior!

== Type Aliases

[cols="1,4"]
|===
| Name| Description
| xref:absl/AnyInvocable/absl_internal_is_view.adoc[`absl&lowbar;internal&lowbar;is&lowbar;view`] 
| Tag indicating that `AnyInvocable` is not a view&hyphen;like type&period;
| xref:absl/AnyInvocable/result_type.adoc[`result&lowbar;type`] 
| The return type of Sig&period;
|===


== Member Functions

[cols="1,4"]
|===
| Name| Description
| xref:absl/AnyInvocable/2constructor-01.adoc[`AnyInvocable`]         [.small]#[constructor]#
| Constructors
| xref:absl/AnyInvocable/2destructor.adoc[`&#126;AnyInvocable`] [.small]#[destructor]#
| If not empty, destroys the target&period;
| xref:absl/AnyInvocable/operator_assign-0b.adoc[`operator&equals;`] 
| Assignment operators
| xref:absl/AnyInvocable/swap.adoc[`swap`] 
| absl&colon;&colon;AnyInvocable&colon;&colon;swap()
| xref:absl/AnyInvocable/2conversion.adoc[`operator bool`] 
| absl&colon;&colon;AnyInvocable&colon;&colon;operator bool()
|===


== Using Declarations

[cols="1"]
|===
| Name
| xref:absl/AnyInvocable/operator().adoc[`operator()`] 
|===


== Friends

[cols="1,4"]
|===
| Name| Description
| `xref:absl/internal_any_invocable/CoreImpl.adoc[absl::internal&lowbar;any&lowbar;invocable::CoreImpl]`
| Grants friendship to other `AnyInvocable` instantiations so they can access this target during converting construction and assignment&period;
| `xref:absl/swap-0f9.adoc[absl::swap]`
| swap()
| `xref:absl/operator_not_eq-0564.adoc[absl::operator!&equals;]`
| Returns `false` if `f` is empty&period;
| `xref:absl/operator_not_eq-049.adoc[absl::operator!&equals;]`
| Returns `false` if `f` is empty&period;
| `xref:absl/operator_eq-0ec.adoc[absl::operator&equals;&equals;]`
| Returns `true` if `f` is empty&period;
| `xref:absl/operator_eq-0e9.adoc[absl::operator&equals;&equals;]`
| Returns `true` if `f` is empty&period;
|===


[.small]#Created with https://www.mrdocs.com[MrDocs]#
