vendredi 21 juillet 2017

mutiple virtual inheritance and variadic template

In my project I played with the following design :

enum {
    A = 1, B = 2, C = 4
};
struct Foo { int foo; };
template <int> struct Bar;
template <> struct Bar<A> : public virtual Foo { int a; };
template <> struct Bar<B> : public virtual Foo { int b; };
template <> struct Bar<C> : public virtual Foo { int c; };

Now I can do something fun like :

template <> struct Bar<A|B> : public A, public B {};
template <> struct Bar<A|C> : public A, public C {};
template <> struct Bar<B|C> : public B, public C {};
template <> struct Bar<A|B|C> : public A, public B, public C {};

So that I can write :

Bar<A|C> bar;
bar.foo = 2;
bar.a = 1;
bar.c = 2;

Now I would like the generation of the combination classes Bar<X|Y|Z|..> to be automatically done when the user creates such an instance. Is this possible using some template magic ?

Something along the lines of :

template <int N, class ...Classes> struct Bar<N> : public Classes... {};
template <int N> struct Bar<N> : public Bar<N, generate_classes<N> > {};

where generate_classes would be able to generate the list of classes Bar<N> should inherit from.

Any help is appreciated !

Aucun commentaire:

Enregistrer un commentaire