What is a callable object in C++?
A callable object is something that can be called like a function, with the syntax object()
or object(args)
; that is, a function pointer, or an object of a class type that overloads operator()
.
The overload of operator()
in your class makes it callable.
There are two steps here. In the C++ Standard, a "function object" is an object that can appear on the left-hand side of a parenthesized argument list, i.e, a pointer to function or an object whose type has one or more operator()
s. The term "callable object" is broader: it also includes pointers to members (which can't be called with the normal function call syntax). Callable objects are the things that can be passed to std::bind
etc. See 20.8.1 [func.def] and 20.8[function.objects]/1.
A callable object is an object instance from a class with operator()
overloaded:
struct Functor { ret_t operator()(); // ...}Functor func; // func is a callable object
or a dereferenced-function pointer:
ret_t func() { // ...}func; // func converts to a callable object