Jump to content

  • Log In with Google      Sign In   
  • Create Account

"Vector" of types? (compile time template)


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
2 replies to this topic

#1 Waterlimon   Crossbones+   -  Reputation: 2635

Like
0Likes
Like

Posted 23 March 2014 - 12:40 PM

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


Sponsor:

#2 Bregma   Crossbones+   -  Reputation: 5422

Like
2Likes
Like

Posted 23 March 2014 - 02:26 PM

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

#3 rozz666   Members   -  Reputation: 634

Like
3Likes
Like

Posted 23 March 2014 - 03:00 PM

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.






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS