Function objects
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.
Function invocation
The exposition-only operation Let type
|
(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) |
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) |
(C++23) |
move-only wrapper of any callable object that supports qualifiers in a given call signature (class template) |
(C++26) |
copyable wrapper of any copy constructible callable object that supports qualifiers in a given call signature (class template) |
(C++26) |
non-owning wrapper of any callable object (class template) |
(C++11) |
the exception thrown when invoking an empty std::function (class) |
(C++11) |
creates a function object out of a pointer to a member (function template) |
Identity
std::identity is the identity function object: it returns its argument unchanged.
(C++20) |
function object that returns its argument unchanged (class) |
Partial function application
std::bind_front and std::bind provide support for partial function application, i.e. binding arguments to functions to produce new functions.
(C++20)(C++23) |
bind a variable number of arguments, in order, to a function object (function template) |
(C++11) |
binds one or more arguments to a function object (function template) |
(C++11) |
indicates that an object is std::bind expression or can be used as one (class template) |
(C++11) |
indicates that an object is a standard placeholder or can be used as one (class template) |
Defined in namespace
std::placeholders | |
(C++11) |
placeholders for the unbound arguments in a std::bind expression (constant) |
Negators
std::not_fn creates a function object that negates the result of the callable object passed to it.
(C++17) |
creates a function object that returns the complement of the result of the function object it holds (function template) |
Searchers
Searchers implementing several string searching algorithms are provided and can be used either directly or with std::search.
(C++17) |
standard C++ library search algorithm implementation (class template) |
(C++17) |
Boyer-Moore search algorithm implementation (class template) |
Boyer-Moore-Horspool search algorithm implementation (class template) |
Reference wrappers
Reference wrappers allow reference arguments to be stored in copyable function objects:
(C++11) |
CopyConstructible and CopyAssignable reference wrapper (class template) |
(C++11)(C++11) |
creates a std::reference_wrapper with a type deduced from its argument (function template) |
(C++20)(C++20) |
get the reference type wrapped in std::reference_wrapper (class template) |
Transparent function objectsAssociative containers and unordered associative containers(since C++20) provide heterogeneous lookup and erasure(since C++23) operations, but they are only enabled if the supplied function object type All transparent function object types in the standard library defines a nested type |
(since C++14) |
Operator function objects
C++ defines the following function objects that represent common arithmetic and logical operations.
The void specializations deduce their parameter types and return types from their arguments, they are all transparent. |
(since C++14) |
Arithmetic operations | |
function object implementing x + y (class template) | |
(C++14) |
function object implementing x + y deducing parameter and return types (class template specialization) |
function object implementing x - y (class template) | |
(C++14) |
function object implementing x - y deducing parameter and return types (class template specialization) |
function object implementing x * y (class template) | |
(C++14) |
function object implementing x * y deducing parameter and return types (class template specialization) |
function object implementing x / y (class template) | |
(C++14) |
function object implementing x / y deducing parameter and return types (class template specialization) |
function object implementing x % y (class template) | |
(C++14) |
function object implementing x % y deducing parameter and return types (class template specialization) |
function object implementing -x (class template) | |
(C++14) |
function object implementing -x deducing parameter and return types (class template specialization) |
Comparisons | |
function object implementing x == y (class template) | |
(C++14) |
function object implementing x == y deducing parameter and return types (class template specialization) |
function object implementing x != y (class template) | |
(C++14) |
function object implementing x != y deducing parameter and return types (class template specialization) |
function object implementing x > y (class template) | |
(C++14) |
function object implementing x > y deducing parameter and return types (class template specialization) |
function object implementing x < y (class template) | |
(C++14) |
function object implementing x < y deducing parameter and return types (class template specialization) |
function object implementing x >= y (class template) | |
(C++14) |
function object implementing x >= y deducing parameter and return types (class template specialization) |
function object implementing x <= y (class template) | |
(C++14) |
function object implementing x <= y deducing parameter and return types (class template specialization) |
Logical operations | |
function object implementing x && y (class template) | |
(C++14) |
function object implementing x && y deducing parameter and return types (class template specialization) |
function object implementing x || y (class template) | |
(C++14) |
function object implementing x || y deducing parameter and return types (class template specialization) |
function object implementing !x (class template) | |
(C++14) |
function object implementing !x deducing parameter and return types (class template specialization) |
Bitwise operations | |
function object implementing x & y (class template) | |
(C++14) |
function object implementing x & y deducing parameter and return types (class template specialization) |
function object implementing x | y (class template) | |
(C++14) |
function object implementing x | y deducing parameter and return types (class template specialization) |
function object implementing x ^ y (class template) | |
(C++14) |
function object implementing x ^ y deducing parameter and return types (class template specialization) |
(C++14) |
function object implementing ~x (class template) |
(C++14) |
function object implementing ~x deducing parameter and return types (class template specialization) |
Constrained comparison function objectsThe following comparison function objects are constrained.
All these function objects are transparent.
|
(since C++20) |
Helper itemsFollowing exposition-only items are used for several components in the standard library but they are not part of the interface of the standard library.
|
(since C++26) |
Old binders and adaptorsSeveral utilities that provided early functional support are deprecated and removed:
|
(until C++20) |
Defect reports
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
DR | Applied to | Behavior as published | Correct behavior |
---|---|---|---|
LWG 185 | C++98 | using function objects improved the program efficiency | removed the claim |
LWG 660 | C++98 | function objects for bitwise operations are missing | added |
LWG 2149 | C++98 | function objects taking one or two arguments were required to provide nested types to denote the argument and result types |
not required |
LWG 2219 | C++11 | INVOKE did not handle std::reference_wrapper correctly
|
handles correctly |
LWG 2420 | C++11 | INVOKE<R> did not discard the return value if R is void
|
discards the return value in this case |
LWG 2926 (P0604R0) |
C++11 | the syntax of the INVOKE operation with a returntype R was INVOKE (f, t1, t2, ..., tN, R)
|
changed toINVOKE<R> (f, t1, t2, ..., tN)
|
LWG 3655 | C++11 | INVOKE did not handle unions correctlydue to the resolution of LWG issue 2219 |
handles correctly |