From cppreference.com
A
type is the type of an object that can be used on the left of the function call operator.
Requirements
The type T satisfies FunctionObject if
The type T satisfies
, and
Given
f, a value of type T or const T,
args, suitable argument list, which may be empty.
The following expressions must be valid:
ExpressionRequirements f(args)performs a function call Notes
Functions and references to functions are not function object types, but can be used where function object types are expected due to function-to-pointer
.
Standard library
All
satisfy this requirement.
All function objects defined in
.
Some return types of functions of
.
Example
Demonstrates different types of function objects.
Run this code
#include<functional>#include<iostream>voidfoo(intx){std::cout<<"foo("<<x<<")\n";}voidbar(intx){std::cout<<"bar("<<x<<")\n";}intmain(){void(*fp)(int)=foo;fp(1);// calls foo using the pointer to functionstd::invoke(fp,2);// all FunctionObject types are Callableautofn=std::function(foo);// see also the rest of <functional>fn(3);fn.operator()(3);// the same effect as fn(3)structS{voidoperator()(intx)const{std::cout<<"S::operator("<<x<<")\n";}}s;s(4);// calls s.operator()s.operator()(4);// the same as s(4)autolam=[](intx){std::cout<<"lambda("<<x<<")\n";};lam(5);// calls the lambdalam.operator()(5);// the same as lam(5)structT{usingFP=void(*)(int);operatorFP()const{returnbar;}}t;t(6);// t is converted to a function pointerstatic_cast<void(*)(int)>(t)(6);// the same as t(6)t.operatorT::FP()(6);// the same as t(6) }Output:
foo(1) foo(2) foo(3) foo(3) S::operator(4) S::operator(4) lambda(5) lambda(5) bar(6) bar(6) bar(6) See also
a type for which the invoke operation is defined
(named requirement)