By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,885 Members | 1,072 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,885 IT Pros & Developers. It's quick & easy.

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

P: n/a
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!
Jul 23 '05 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.