Boost logo

Boost Users :

From: Aleksey Gurtovoy (agurtovoy_at_[hidden])
Date: 2005-05-05 19:38:18


Rick Sloan writes:
> Hello all,
>
> Given a mpl::list
> typedef mpl::list<structA, structB, structC> typeList;
> and a wrapper struct
> template <class T>
> struct wrap
> {
> virtual void someFunction(T&) = 0;
> virtual ~wrap
> }
>
> You can generate a class by
> mpl::inherit_linearly<typeList, mpl::inherit<wrap<_2>,_1> >:;type generated;
>
> This is pretty much straight from "C++ Template Metaprogramming" pg. 193-194.
>
> My question is how do you generate the derived concrete class? I can
> override any particular function by
>
> struct Child : public generated
> {
> virtual void someFunction(structA&);
> }
>
> but I can't seem to generate an entire class.

1) Unless there is a specific reason to prefer "mixing in" 'wrap<T>'
nodes (i.e. building 'inherit< wrap<T1>,inherit< wrap<T2>,...
inherit<wrap<Tn>,empty_base> ...> >') to "chaining" them into a linear
hierarchy ('wrap<T1,wrap<T2,...wrap<Tn,empty_base> ...> >'), I'd
recommend going with the latter:

   struct base
   {
       virtual base() {}
       virtual void someFunction(T&) {}
   };

   template <class T, class Base>
   struct wrap : Base
   {
       using Base::someFunction; // bring in Base overloads
       virtual void someFunction(T&) = 0;
   };

   typedef mpl::inherit_linearly<
         types
       , wrap<_2,_1>
       , base
>::type wrap_interface;

2) If I understood you question correctly, here's one way
to generate a matching implementation for 'wrap_interface':

   template <class T, class Base>
   struct wrap_impl : Base
   {
       virtual void someFunction(T& x)
       {
           some_function_impl(x);
       }
   };

   typedef mpl::inherit_linearly<
         types
       , wrap_impl<_2,_1>
       , wrap_interface
>::type wrap_impl;

   void some_function_impl(T1& x ) { /* actual implementation for T1 */ }
   void some_function_impl(T2& x ) { /* actual implementation for T2 */ }
   ...

HTH,

-- 
Aleksey Gurtovoy
MetaCommunications Engineering

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