Skip to content
Tutorials Center
My learning

Topics

C++ Reference ยท Types and Language Concepts

Function objects (functors) in C++

Reference for C++ function objects: classes with operator(), stateful functors, standard functors such as std::plus and std::less, and std::function.

A function object, or functor, is any object that can be called with function-call syntax. In the simplest case it is an instance of a class that defines operator(). Because it is an object, it can hold state, and because its type is known at compile time, the compiler can inline the call, which is why algorithms with functors are as fast as hand-written loops.

Lambdas, function pointers, std::function, std::bind results and the functors in <functional> are all function objects. Algorithms and containers accept all of them through template parameters, so you choose what is most readable.

C++
struct IsEven {
    bool operator()(int n) const { return n % 2 == 0; }
};
bool r = IsEven{}(4);   // true

Try it

C++ Stateless and stateful functors

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++ Standard functors and std::function

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++
struct F {
    R operator()(Args...) const;   // callable object
};

std::function<R(Args...)> f = callable;
Standard functor (<functional>) Meaning
std::plus, minus, multiplies, divides, modulus, negate Arithmetic.
std::equal_to, not_equal_to, less, greater, less_equal, greater_equal Comparison.
std::logical_and, logical_or, logical_not Logic.
std::bit_and, bit_or, bit_xor, bit_not Bitwise.
std::function, std::move_only_function (C++23) Type-erased wrappers for any callable.
std::ref, std::cref, std::bind_front, std::not_fn, std::mem_fn Adapters.

Writing std::less<> (with the empty argument list, "transparent" functors) deduces the types and compares mixed types.

Usage notes

  • Make operator() const when it does not change state. Many algorithms and containers need a const-callable comparator or predicate.
  • Algorithms copy functors. std::for_each(first, last, f) and most others take the function object by value, so state changes happen in a copy. Use std::ref(f), or use the return value of for_each, to observe the state.
  • Predicates should be pure (the same result for the same argument, no hidden state), because algorithms may copy them or call them in any order and more than once.
  • Functor versus lambda. A lambda is a compact functor class generated for you. Write a class when the object needs several functions, a name, or reuse across files.
  • Functor versus function pointer. A pointer hides the callee from the optimizer; a functor type is part of the template instantiation and can be inlined.
  • std::function costs. It type-erases, may allocate for large callables, and requires copyable callables (use std::move_only_function in C++23 for move-only ones). Prefer a template parameter if you do not need to store it.
  • std::bind is mostly replaced by lambdas; std::bind_front (C++20) is the simple case.
  • Overloaded operator() is allowed. A functor with several overloads (the "overloaded" idiom) is useful with std::visit.
  • Removed: std::unary_function and std::binary_function were deprecated in C++11 and removed in C++17.