Game Development Reference
In-Depth Information Handling
The good news is that C++11 has introduced two very useful constructs that will greatly
simplify this system, std::function and std::bind . The definition of std::function is a
general-purpose polymorphic function wrapper; it allows us to store, copy and invoke any
function, including lambda expressions and function objects.
std::function is a general-purpose wrapper that can store, copy and invoke any callable tar-
template <typename T>
class event_handler
typedef std::function<void(void*, T)> signature_t;
typedef std::list<signature_t> functionList_t;
typename functionList_t::iterator operator += (signature_t callback)
return m_callbacks.begin();
void operator -= (typename functionList_t::iterator& it)
void Invoke(void* sender, T data)
for (auto& callback : m_callbacks )
callback(sender, data);
functionList_t m_callbacks;
The event_handler class keeps a list of callbacks, this means that we can register any num-
ber of functions to be invoked should the event occur, conversely we have the ability to
unregister a callback should one of the owners of a callback goes out of scope or gets re-
leased, we need to avoid calling a function on an object that has been destroyed.
As we mentioned, the std::function is a wrapper that supports anything that behaves as a
callable function, including lambda expressions as you can see in the following code:
event_handler<int> handler;
Search WWH ::

Custom Search