"Vector" of types? (compile time template)

Started by
1 comment, last by rozz666 10 years, 1 month ago

I was wondering, is it possible to make a list of types using template programming, with methods to iterate over them etc.?

Essentially im looking for an equivalent of a simple std container except for arbitrary types and defined at compile time.

I want to do compile time polymorphism.

Something like:


GameObjectContainer<Contained<Player, MAP>, Contained<Tree, VECTOR>> objects;
*For each type of game object i can define what kind of data structure is most suitable in a compact form. The non template alternative is manually defining these things within the game object container, as well as manually updating the objects.update() etc. to account for new containers to go through.

int index1=objects.add<Player>();
*Adds to MAP<Player>

int index2=objects.add<Tree>();
*Adds to VECTOR<Tree>

Player p=objects.get<Player>(index1);
*Accesses map


Tree t=object.get<Tree>(index2);
*Accesses vector


objects.update();
*Goes through both map and vector automatically, i never need to update how this method works.

As you can see this would be only to reduce the amount of code in well defined situations. How would i implements something like this in the cleanest way?

Is there a C++ object that can contain a list of types and has a for_each method that runs a lambda for each element (with each element being of different type, im not sure if lambdas work for this?)

o3o

Advertisement
You need to read Alexandrescu's "Modern C++ Design." It's a little dated now (it was written to the 1997 C++ standard) but it will still work with modern compilers. It spend most of its bulk dealing with template typelists.

Stephen M. Webb
Professional Free Software Developer

Have a look at Boost.MPL: http://www.boost.org/doc/libs/1_55_0/libs/mpl/doc/refmanual.html

It provides among other things containers and algorithms for types.

This topic is closed to new replies.

Advertisement