• Advertisement
Sign in to follow this  

"Vector" of types? (compile time template)

This topic is 1398 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?)

 
 
 

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement