15 Aug 2012 15:49
[Interest] Value Dispatch
<*smgreened <at> obbligato.org>
2012-08-15 13:49:35 GMT
2012-08-15 13:49:35 GMT
Hi all,
This might be old hat to verteran boosters but I found it nifty. Maybe
it could make a small addition to Boost.
Inspired by Sean Parent's C++Now! talk and the various articles on
cpp-next, I've been playing around with value semantics and C++11.
I found an interesting use of pack expansion at stackoverflow and
expanded on it in a local project to convert from a traditional
inheritance/virtual function model to a more value-centered approach.
It turns out that with pack expansion one can implement "virtual
functions" without needing inheritance or pointer/reference semantics.
One can consider a vrtual function as "just" a compiler-generated switch
statement. With that in mind, here's an example:
-----------
#include <iostream>
/// This is a collection of node kinds. We use it to categorize
/// various kinds of objects and dispatch to the correct routine.
template<typename T, typename T::kind ...I>
class kind_tuple {};
/// This is the "virtual function" dispatch mechanism. It
/// statically generates a lookup table of routines, one for each
/// kind.
template<typename R, typename D, typename T>
(Continue reading)
RSS Feed