Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing typedef from the instance

As in stl containers, why can't we access a typedef inside the class from the class instance? Is there a particular insight into this?


When value_type was a template parameter it could help making more general code if there wasn't the need to specify the template parameters as in vector::value_type

Example:

class T {
public:
    typedef int value_type;
    value_type i;
};

T t;
T::value_type i; // ok
t.value_type i;  // won't work
like image 565
piotr Avatar asked Jun 18 '10 09:06

piotr


1 Answers

The answer is use decltype to get the class first. E.g.,

decltype(t)::value_type

Requires C++11.

Reference: https://stackoverflow.com/a/13936644/577704

like image 134
Ivan Xiao Avatar answered Oct 12 '22 23:10

Ivan Xiao