Rick Sloan writes:
Hello all,
Given a mpl::list
typedef mpl::list 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,_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 ...> >') to "chaining" them into a linear
hierarchy ('wrap ...> >'), I'd
recommend going with the latter:
struct base
{
virtual base() {}
virtual void someFunction(T&) {}
};
template
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
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