Class Composition
#include <boost/mpl/inherit.hpp> #include <boost/mpl/inherit_linearly.hpp> #include <boost/lambda/construct.hpp> namespace pst { namespace mpl = boost::mpl; namespace lam = boost::lambda; using mpl::_1; using mpl::_2; template <typename Sequence> struct factory : mpl::inherit_linearly< Sequence, mpl::inherit< _1, lam::new_ptr<_2> > >::type { }; } // pst
#include <string> #include <list> #include <boost/mpl/vector.hpp> int main() { using namespace std; namespace mpl = boost::mpl; namespace lam = boost::lambda; typedef mpl::vector<string,list<int>,char> t_types; pst::factory<t_types> a_factory; string* ps = static_cast< lam::new_ptr<string> >(a_factory)(); list<int>* pl = static_cast< lam::new_ptr< list<int > > >(a_factory)(); char* pc = static_cast< lam::new_ptr<char> >(a_factory)(); return 0; }
letsboost::mpl
C++ Template Metaprogramming Section 9.5