0Pricing
C++ Academy · Lesson

Choosing Callables

Lambdas vs bind vs pointers.

The Callable Toolbox

C++ offers several ways to pass behavior around: function pointers, lambdas, functors, std::bind, and std::function. Each fits different needs.

  • Speed vs flexibility is the usual tradeoff.
  • Readability matters too.
#include <iostream>

int twice(int x) { return x * 2; }

int main() {
    int (*fp)(int) = twice;
    auto lam = [](int x) { return x * 2; };
    std::cout << fp(5) << ' ' << lam(5) << '\n';
    return 0;
}

When to Use a Function Pointer

Reach for a function pointer when you need a tiny, stateless callback and want C compatibility or zero overhead.

#include <iostream>

void logMsg(const char* m) { std::cout << "LOG: " << m << '\n'; }

int main() {
    void (*handler)(const char*) = logMsg;
    handler("started");
    return 0;
}

All lessons in this course

  1. Function Pointers
  2. std::function
  3. std::bind
  4. Choosing Callables
← Back to C++ Academy