0Pricing
C++ Academy · Lesson

Function Objects Functors

Write classes with operator() to create stateful callable objects.

Function Objects Functors is a free C++ Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C++ Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

What is a Functor?

A functor (function object) is a class that overloads operator(). Instances can be called like functions.

struct Square {
    int operator()(int x) const { return x * x; }
};

Square sq;
std::cout << sq(5);   // 25

Why Not Just a Function?

Functors carry state. A plain function does not. State enables configuration, counting, and stateful callbacks.

struct Counter {
    int count = 0;
    int operator()(int x) { ++count; return x; }
};

Counter c;
c(10); c(20); c(30);
std::cout << c.count;   // 3

Configurable Behavior

Store configuration in the functor s members. The same functor type works for many parameter sets.

struct GreaterThan {
    int threshold;
    bool operator()(int x) const { return x > threshold; }
};

GreaterThan over10{10};
over10(15);    // true
over10(5);     // false

Using Functors with STL Algorithms

STL algorithms accept anything callable — functors, lambdas, function pointers.

std::vector<int> v = {1, 11, 5, 99, 8};
auto it = std::find_if(v.begin(), v.end(), GreaterThan{10});
std::cout << *it;   // 11

Predefined Functors in <functional>

The standard library provides ready-made functors: std::plus, std::less, std::equal_to, std::negate, and more.

#include <functional>
std::vector<int> v = {3, 1, 4, 1, 5};
std::sort(v.begin(), v.end(), std::greater<int>{});
// sorted descending

Functor vs Function Pointer

Functors usually inline better because their operator() is a direct call. Function pointers can be more flexible but indirect.

Functors as Template Parameters

Many STL containers and algorithms accept a functor as a template parameter — set comparators, hash functions, allocators.

std::set<int, std::greater<int>> s = {1, 2, 3};
// s iterates in descending order: 3, 2, 1

Lambdas Are Functors

The compiler generates a unique class with operator() for every lambda. Lambdas are syntactic sugar over functors.

When to Write a Functor

Use a hand-written functor when:

  • The same logic is used many places (give it a name)
  • It is part of your public API
  • It needs to be copy/move constructible with specific behavior

When to Use a Lambda Instead

For one-off usage, a lambda is shorter and clearer. For reusable behavior, a named functor improves readability.

Composability

Combine functors using std::bind or, more modernly, by composing lambdas. Functional libraries like Boost provide combinators too.

Quick Check

What is the main capability of a functor compared to a plain function?

Recap

A functor is a class with operator(). It carries state, integrates with STL algorithms, and serves as the foundation for lambdas. Use named functors for reusable behavior, lambdas for one-shot logic.

Frequently asked questions

Is the “Function Objects Functors” lesson free?

Yes — the full text of “Function Objects Functors” is free to read here on the web, and the C++ Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C++ Academy course, upgrade to CoddyKit PRO.

What will I learn in “Function Objects Functors”?

Write classes with operator() to create stateful callable objects. You practise C++ Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start C++ Academy?

No prior experience is required. C++ Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Function Objects Functors” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this C++ Academy lesson?

Yes. Every C++ Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Function Objects Functors
  2. Lambda Captures by-value by-reference mutable
  3. Generic Lambdas and Closures C++14
  4. std::function Type-erased Callables
← Back to C++ Academy