C/C++ Arena

Step 7 of 7

Challenge: callbacks with std::function

std::function<R(Args...)> holds any callable with that signature: a plain function, a lambda (including one with captures), or an object with operator(). It's C++'s answer to the C function-pointer-plus-void* pattern from the C modules, but type-safe.

std::function<void(const std::string&)> handler = [&count](const std::string&) { count++; };
handler("boom");

Your turn: build an EventBus where handlers subscribe to named events:

Previous: Iterator invalidation