Namespaces
Variants
Actions

Function objects

From cppreference.com
< cpp‎ | utility
 
 
Utilities library
General utilities
Function objects
Bit manipulation (C++20)
C-style bit manipulation (C++26)
(C++11)
Relational operators (deprecated in C++20)
 
Function objects
Function invocation
(C++17)(C++23)
Identity function object
(C++20)
Transparent operator wrappers
(C++14)
(C++14)
(C++14)
(C++14)  
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)

Old binders and adaptors
(until C++17*)
(until C++17*)
(until C++17*)
(until C++17*)  
(until C++17*)
(until C++17*)(until C++17*)(until C++17*)(until C++17*)
(until C++20*)
(until C++20*)
(until C++17*)(until C++17*)
(until C++17*)(until C++17*)

(until C++17*)
(until C++17*)(until C++17*)(until C++17*)(until C++17*)
(until C++20*)
(until C++20*)
 

A function object is any object for which the function call operator is defined. C++ provides many built-in function objects as well as support for creation and manipulation of new function objects.

Contents

[edit] Function invocation

The exposition-only operation INVOKE(f, arg_0, arg_1, arg_2, ..., arg_N) is defined as follows:

Let type Obj be the unqualified type of arg_0 (i.e., std::remove_cv<std::remove_reference<decltype(arg_0)>::type>::type)

  • (obj.*f)(arg_1, arg_2, ..., arg_N) (invoke the member function on the object).
  • (obj.get().*f)(arg_1, arg_2, ..., arg_N) (invoke the member function on the specially referred object).
  • Otherwise
  • ((*obj).*f)(arg_1, arg_2, ..., arg_N) (invoke the member function on the dereferenced object).
  • obj.*mptr (access the data member of the object).
  • obj.get().*mptr (access the data member of the specially referred object).
  • Otherwise
  • (*obj).*mptr (access the data member of the dereferenced object).
  • Otherwise
  • INVOKE(f, arg_0, arg_1, arg_2, ..., arg_N) is equivalent to f(arg_0, arg_1, arg_2, ..., arg_N) (invoke the callable).


The exposition-only operation INVOKE<R>(f, arg_0, arg_1, arg_2, ..., arg_N) is defined as follows:

  • If R is (possibly cv-qualified) void
  • static_cast<void>(INVOKE(f, arg_0, arg_1, arg_2, ..., arg_N)).
  • Otherwise
  • INVOKE(f, arg_0, arg_1, arg_2, ..., arg_N) implicitly converted to R.

Let type Actual be decltype(INVOKE(f, arg_0, arg_1, arg_2, ..., arg_N))

  • INVOKE<R>(f, arg_0, arg_1, arg_2, ..., arg_N) is ill-formed.
(since C++23)
(since C++11)


std::invoke and std::invoke_r(since C++23) can invoke any Callable object with given arguments according to the rules of INVOKE and INVOKE<R>(since C++23).

(C++17)(C++23)
invokes any Callable object with given arguments and possibility to specify return type(since C++23)
(function template) [edit]

[edit] Function wrappers

These polymorphic wrapper classes provide support for storing arbitrary function objects.

(C++11)
copyable wrapper of any copy constructible callable object
(class template) [edit]
move-only wrapper of any callable object that supports qualifiers in a given call signature
(class template) [edit]
copyable wrapper of any copy constructible callable object that supports qualifiers in a given call signature
(class template) [edit]
non-owning wrapper of any callable object
(class template) [edit]
the exception thrown when invoking an empty std::function
(class) [edit]