libstdc++
|
Modules | |
Adaptors for pointers to functions | |
Adaptors for pointers to members | |
Arithmetic Function Object Classes | |
Binder Classes | |
Boolean Operations Classes | |
Comparison Classes | |
Hashes | |
Negators | |
Functions | |
template<typename _Tp , typename _Class > | |
constexpr _Mem_fn< _Tp _Class::* > | std::mem_fn (_Tp _Class::*__pm) noexcept |
template<typename _Fn > | |
constexpr auto | std::not_fn (_Fn &&__fn) noexcept(std::is_nothrow_constructible< std::decay_t< _Fn >, _Fn && >::value) |
Function objects, or functors, are objects with an operator()
defined and accessible. They can be passed as arguments to algorithm templates and used in place of a function pointer. Not only is the resulting expressiveness of the library increased, but the generated code can be more efficient than what you might write by hand. When we refer to functors, then, generally we include function pointers in the description as well.
Often, functors are only created as temporaries passed to algorithm calls, rather than being created as named variables.
Two examples taken from the standard itself follow. To perform a by-element addition of two vectors a
and b
containing double
, and put the result in a
, use
To negate every element in a
, use
The addition and negation functions will usually be inlined directly.
An adaptable function object is one which provides nested typedefs result_type
and either argument_type
(for a unary function) or first_argument_type
and second_argument_type
(for a binary function). Those typedefs are used by function object adaptors such as bind2nd
. The standard library provides two class templates, unary_function
and binary_function
, which define those typedefs and so can be used as base classes of adaptable function objects.
Since C++11 the use of function object adaptors has been superseded by more powerful tools such as lambda expressions, function<>
, and more powerful type deduction (using auto
and decltype
). The helpers for defining adaptable function objects are deprecated since C++11, and no longer part of the standard library since C++17. However, they are still defined and used by libstdc++ after C++17, as a conforming extension.
|
inlineconstexprnoexcept |
Returns a function object that forwards to the member pointer pointer pm
.
This allows a pointer-to-member to be transformed into a function object that can be called with an object expression as its first argument.
For a pointer-to-data-member the result must be called with exactly one argument, the object expression that would be used as the first operand in a obj.*memptr
or objp->*memptr
expression.
For a pointer-to-member-function the result must be called with an object expression and any additional arguments to pass to the member function, as in an expression like (obj.*memfun)(args...)
or (objp->*memfun)(args...)
.
The object expression can be a pointer, reference, reference_wrapper
, or smart pointer, and the call wrapper will dereference it as needed to apply the pointer-to-member.
Definition at line 234 of file functional.
|
inlineconstexprnoexcept |
Wrap a function object to create one that negates its result.
The function template std::not_fn
creates a "forwarding call wrapper", which is a function object that wraps another function object and when called, forwards its arguments to the wrapped function object.
The result of invoking the wrapper is the negation (using !
) of the wrapped function object.
Definition at line 1097 of file functional.