getting a type from an object

AFAIR, it is not possible to get a type from an object, only from a type. Even with auto, you can only instantiate another type of the same type.

Is this still the case?

I'd like to get a type from an object given another type, mainly because it is less typing and less error prone.

Some psudocode:

1
2
3
Type1 A<X, Y> a;
int b;
Type2 a.InnerType<b>::type c; // obviously won't work 


A work around I thought of would be to use functions:
1
2
3
Type1 A<X, Y> a;
int b;
auto c = a.getInnerType(b); 


Which is kinda interesting and looks like the way I'm going to go. However, I'm just wondering if there is any other ways of doing this as I don't really want to instantiate an object if I can avoid it (though the optimizer will probably just dump it anyway).

Thanks,


A
Last edited on
it is not possible to get a type from an object


This is exactly what C++11's decltype does:

 
decltype(a) c;  // 'c' will be of object of the same type as 'a' 
Ahhh, forgot about that keyword. Will still need a fake function to get the type I want but slightly better than auto.

Thanks,


A
Topic archived. No new replies allowed.