0Pricing
C++ Academy · Lesson

std::function Type-erased Callables

Erase types behind std::function and understand the small-buffer optimization.

std::function Type-erased Callables is a free C++ Academy lesson on CoddyKit — lesson 4 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.

The Problem: One Type for Many Callables

Each lambda has a unique type. Each function pointer is a different type. We need one type that can hold any of them — std::function.

Template Parameter

std::function<Signature> takes the return type and parameter types. It accepts anything callable with that signature.

#include <functional>
std::function<int(int, int)> op;
op = [](int a, int b) { return a + b; };
std::cout << op(3, 4);   // 7

Storing Different Callables

The same std::function variable can hold a lambda, a function pointer, or a functor — as long as the signature matches.

std::function<int(int)> f;
f = [](int x) { return x * 2; };       // lambda
f = ::abs;                              // function pointer
f = std::negate<int>{};                  // functor

Type Erasure

Internally, std::function uses type erasure — it stores any callable and hides its real type behind a uniform interface. Often implemented with a small heap allocation.

Performance Cost

Calling through std::function involves an indirect call (similar to a virtual function). Slower than a direct lambda call. Avoid in hot paths.

When to Use std::function

Use it when:

  • Storing callbacks in a container or class member
  • Returning a callable from a function
  • Building a plugin or event system

Empty std::function

A default-constructed std::function is empty. Calling it throws std::bad_function_call.

std::function<void()> f;
if (f) f(); else std::cout << "no callback set";

Member Function Pointers

Bind a member function with std::bind or wrap in a lambda.

struct Obj { void greet(int) {} };
Obj o;
std::function<void(int)> cb = [&](int n) { o.greet(n); };

Comparison with Function Pointers

Function pointers cannot store state. std::function can — at the cost of indirect calls.

Comparison with Templates

Templates with callable parameters specialize at compile time — fastest. std::function resolves at runtime — slower but flexible.

template <typename F>
void run(F f) { f(); }            // compile-time, no overhead

void run(std::function<void()> f) { f(); }   // runtime, type erasure

std::function in C++23: std::function_ref

C++23 added std::function_ref — a non-owning view of a callable. Avoids the heap allocation when the lifetime is bounded.

Alternatives

Lightweight alternatives exist: std::move_only_function (C++23), tl::function_ref, folly::Function. Pick based on needs.

Quick Check

What is the main drawback of std::function?

Recap

std::function<Signature> stores any callable with a matching signature. Useful for callbacks, plugins, and runtime polymorphism over callables — at the cost of indirect calls.

Frequently asked questions

Is the “std::function Type-erased Callables” lesson free?

Yes — the full text of “std::function Type-erased Callables” 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 “std::function Type-erased Callables”?

Erase types behind std::function and understand the small-buffer optimization. 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 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “std::function Type-erased Callables” 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