Boost logo

Boost Users :

From: David Abrahams (dave_at_[hidden])
Date: 2004-07-01 09:13:43


"Klaus Nowikow" <nowikow_at_[hidden]> writes:

> Greetings!
>
> Does boost.mpl provide type hierarchy generators like GenScatterHierarchy
> and GenLinearHierarchy as described in "Modern C++ Design"?
> I must confess I do not understand the mpl completely yet ;-)

Well, yeah. It's just a thin wrapper over mpl::fold

   // inherit_linearly.hpp
   template<
         typename Types_
       , typename Node_
       , typename Root_ = empty_base
>
   struct inherit_linearly
     : fold<Types_,Root_,Node_>
   {
   };

   // inherit.hpp
   template <typename A, typename B>
   struct inherit : A, B {};

So:

 #include <boost/mpl/inherit_linearly.hpp>

 template<class T, class Base>
 struct node;

 typedef mpl::vector<int, char, long> types;

 // node<int, node<char, node<long, mpl::empty_base> > >
 typedef mpl::inherit_linearly<
     types
   , node<_2,_1>
>::type lin;

or:

 #include <boost/mpl/inherit_linearly.hpp>
 #include <boost/mpl/inherit.hpp>

 template <class T>
 struct node;

 // mpl::inherit<
 // node<int>
 // , inherit<
 // node<char>
 // , inherit<
 // node<long>
 // , mpl::empty_base
 // >
 // >
 // >
 typedef mpl::inherit_linearly<
     types
   , mpl::inherit<node<_2>,_1>
>::type scat;

HTH.

-- 
Dave Abrahams
Boost Consulting
http://www.boost-consulting.com

Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net