Jump to content
  • Advertisement
Sign in to follow this  

Templates and variable names

This topic is 3982 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

Hi, I've had an idea on the train home for my engine, but before I get too carried away with it I'd like to know if something is possible with C++ templates... Imagine I have a class like this:
template< typename T, typename U >
class MyClass {
    T var1;
    U var2;
When I instantiate it I can do something like this:
MyClass<House, Car> someInstance;
//here I can access someInstance.var1
What I want to do is name the member variables using some kind of ID from the passed in objects. So, for example..
template<typename T, typename U>
class MyClass {
    T  T::someID; //someID here is house when instantiated below
    U  U::someID; //someID here is car

MyClass<House, Car> someInstance;
//here I can access someInstance.house and someinstance.car
Now, I want to use this for something a lot more complicated than that, but that's the simple case that I want to achieve. I could do something using a map<string, boost::any> and do someInstance.m_map["car"] but I want to move the lookup from runtime to compile time. I know this must be possible (or something equivalent) somehow, I just don't know how [smile]

Share this post

Link to post
Share on other sites
Lo, and behold.

 // Your basic template specialization
template <typename T>
class by_id {};

// A macro to set an identifier.
#define SET_ID(Class,ident) \
template<> class by_id<Class> { \
public: Class ident; \

// Set a few identifiers.

// Now, the actual template:
template <typename T, typename U>
class named_pair : public by_id<T>, public by_id<U> {}

// Perhaps specialize named_pair<T,T> ?

// Usage
named_pair<House, Car> pair;

Disclaimer: this is untested. Also, it's useless.

Share this post

Link to post
Share on other sites
ToohrVyk: That is exactly what I was after, thanks a lot!

the_edd: Boost.Fusion looks interesting thanks for the link.

The reason I needed to know how to do that is I am planning to write a component-based entity system using Variadic Templates that are introduced in TR1 (available in GCC 4.3)

So basically it will work something like this:

Entity< MoveComponent,
ScriptComponent > newEntity;

newEntity.move.setPosition(10, 10, 10);
newEntity.fly.moveUp(100); //Compile-time error, there is no fly component

I've nearly got it working, just need to do a bit more fiddling so I can update each of the components in a loop. But this way it saves on testing for component dependencies (for example the FlyComponent might do parent->move->setPosition() ) and it also saves on runtime lookups in a map. Anyway, it was just an idea I had yesterday which needs some work. [smile]

Share this post

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

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!