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:
int on(const std::string& event, std::function<void(const std::string&)> fn)registers a handler and returns a unique id (1, 2, 3, ...)bool off(int id)unregisters; returns false if the id isn't registeredint emit(const std::string& event, const std::string& payload)calls every handler for that event, in registration order, and returns how many ran