# Templates and variable names

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

## 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 {
public:
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 on other sites
At this point, it'd just be easier to do:
struct MyClassHouseCar {  House house;  Car   car;};

##### Share on other sites
Lo, and behold.

 // Your basic template specializationtemplate <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.SET_ID(House,house);SET_ID(Car,car);// 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> ?// Usagenamed_pair<House, Car> pair;foo(pair.house);bar(pair.car);

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

##### Share on other sites
Boost.Fusion is kinda-sorta meant for this kind of thing (*shudder*).

##### 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,              HealthComponent,             DamageComponent,             ScriptComponent >  newEntity;    newEntity.setType(MONSTER);    newEntity.health.setHealth(100);    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]

1. 1
2. 2
3. 3
4. 4
Rutin
16
5. 5

• 12
• 9
• 12
• 37
• 12
• ### Forum Statistics

• Total Topics
631414
• Total Posts
2999964
×