Skip to content
Tutorials Center
My learning

Topics

C++ Reference ยท Operators

The C++ function call operator ()

Reference for the C++ function call operator operator(): functors, stateful callables, lambdas, const and mutable calls, static operator() and std::invoke.

A class that overloads operator() produces objects you can call like functions: a function object, or functor. Unlike a plain function, a functor can hold state in its members and can carry several overloads, and the compiler can inline its call because the type is known at compile time.

Lambdas are shorthand for such classes: a lambda expression creates an unnamed type with a const operator() (unless you write mutable). The standard algorithms and containers (std::sort, std::map's comparator, std::function) all accept anything callable this way.

C++
struct Adder {
    int n;
    int operator()(int x) const { return x + n; }
};
Adder add5{5};
int y = add5(10);   // 15

Try it

C++ A stateful functor

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

C++ Lambdas, mutable, and static operator()

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

Syntax

C++
ReturnType operator()(Parameters) const;       // member; any number of parameters
static ReturnType operator()(Parameters);      // C++23
ReturnType operator()(this Self&& self, Parameters);   // C++23 explicit object parameter

obj(args...)                                   // call expression
Name Description
Parameters Any number, including default arguments and variadic packs. This is the only overloaded operator that allows default arguments.
const Mark the call operator const unless it must modify members. Algorithms and containers often call functors through a const reference.

Returns: whatever you declare, including void and auto.

Errors / undefined behavior: calling a non-const operator() through a const object or a const reference does not compile. A functor stored in std::function that captured a dangling reference is undefined behavior when called. Calling an empty std::function throws std::bad_function_call.

Usage notes

  • Member only: operator() must be a non-static member (or static since C++23); it cannot be a free function.
  • Copies: standard algorithms take callables by value and may copy them several times, so state kept inside a copy may be lost. Hold shared state by reference, or pass std::ref(f), or use the return value (std::for_each returns the functor).
  • Performance: a functor or lambda passed as a template argument is inlined; a function pointer or std::function often is not. std::function may also allocate.
  • Standard functors: <functional> provides std::plus, std::less, std::greater, std::equal_to and their transparent forms (std::less<>) that compare mixed types.
  • Overload sets: a functor can have several operator() overloads; a plain lambda has only one (combine them with an overloaded helper and std::visit).
  • Static (C++23): static operator() and static lambdas ([](int x) static { ... }) make a captureless callable convertible to a plain function pointer without needing an object.
  • Deduction: a generic lambda ([](auto x)) has a templated operator().
  • Uniform calling: std::invoke(f, args...) works for functions, member pointers and functors alike.