Using template metaprogramming to generate a composite class withID derived from components

  • Thread starter Michael Andersson
  • Start date
M

Michael Andersson

Given a set of classes

class A { enum [ ID = 0x0001} };
class B { enum [ ID = 0x0002} };
class B { enum [ ID = 0x0004} };

I wish to generate a composite class, perhaps using something like
Alexandrescu's typelists

(pseudo-code:)

template<class TList>
class Composite : TList
{
enum {ID = SumID<TList>::result}
};

typedef composite<TYPELIST(A,B,C)> CompABC; // ID == 0x0007

Of course, the trivial A, B and C classes above would have data members
to make Composite meaningful, but the really important issue is
Composite's ID member, the value of which must be unique to the
combinations of components.

Thus, there are two parts to this. One, generating a linear inheritance
hierarchy from the typelist; and two, summing up the components' ID
values as the ID value of the generated composite.

Though I know exactly what I need I've so far (due to my inexperience
with metaprogramming) failed to implement this. I've tried using
Alexandrescu's Loki and BoostMPL, but am stymied. Any help would be
deeply appreciated!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,983
Messages
2,570,187
Members
46,747
Latest member
jojoBizaroo

Latest Threads

Top