The following variadic template recursively creates classes and adds each of the specified members in sequence:
class Dummy {};
template<class Base, typename... Members>
// using Composition = Base;
class Composition : public Base {};
template<class Base, typename FirstMember, typename... MoreMembers>
class Composition<Base, FirstMember, MoreMembers...>
: public Composition<Base, MoreMembers...> {
protected:
FirstMember member;
};
Composition<Composition<
Composition<Dummy, int>, Composition<Dummy, int>*>, double> composition;
It works, but I feel the terminating step is a bit clumsy: It creates a new class Composition<Base> which inherits from Base but does not do anything else. I would prefer if the last step could simply be template<class Base, typename... Members> using Composition = Base; - but that is not allowed because we may not specialize alias templates.
Aucun commentaire:
Enregistrer un commentaire