In C++ we have an operator::
, which allows us to do things like that:
typedef std::pair<A, B> pairAB;
pairAB p;
pairAB::first_type a(42);
pairAB::second_type b("hello world");
p.first = a;
p.second = b;
I tried to use this operator on the object of this type:
pairAB p2;
p2::first_type a2(42);
p2::second_type b2("hello again");
p2.first = a2;
p2.second = b2;
And the compiler showed me error: C2510: 'p2' : left of '::' must be a class/struct/union
, and I want to know is there any trick which allows me to do that?
Getting the type of a variable is exactly what
decltype
keyword is for in c++11.Prior to c++11 there was no standard way to do that. There are compiler specific features such as gcc's
typeof
and library based implementations such as Boost.Typeof that have existed prior to c++11 and do a similar thing.