I have a set of classes that have the following structure:
class U
{
public:
explicit U(int) { ... }
U() {...}
Init(int) {...}
};
I need to be able to compose 1 or more of these classes into a class X. Pseudocode:
template<class TypeSequence>
class X that derives publicly from all the classes in TypeSequence
{
X(int): all bases are initialized with the integer passed
{}
//if the above constructor is impossible, then the following will do as well:
X(int)
{
Call Init on all bases and pass the given int to them.
}
};
I think I need a lot of mpl, but I’m not really good at it. Is what am I trying to do doable? A code sample would be great.
MY FAULT: Forgot to mention I can’t use C++11 features. I am looking for an MPL solution.
Well, Boost.MPL contains metafunctions
inheritandinherit_linearlyyou can combine them withfor_eachto get the second variant (with init functions). Or using justboost::mpl::foldand custom metafunctions:Usage example:
Metafunction
InheritFrom_IntConstructorcan generalized to accept arbitrary type as constructor parameter and I’m not sure if can generalized to accept arbitrary number of arguments.