How to get typedef member from object instance

In MSVC2010, the following code gives:
error C2039: 'my_type' : is not a member of ''global namespace''

template<typename T>
class C
{
public:
    typedef T my_type;
};

C<int> c;

auto f = [&c]() { 
    decltype(c)::my_type v2;   // ERROR C2039
};

I found a lame way around this, but I'm wondering how to get to typedef correctly when you only have an instance of the object.

+4
source share
1 answer

From a conglomerate of very useful comments, I got a working solution. Thanks to all. remove_reference performs a binary assignment as an identity object.

template<typename T>
class C {
public:
  typedef T my_type;
};

void g() {
  C<int> c;

  auto f = [&c]() {
    typedef remove_reference<decltype(c)>::type::my_type my_type;
    my_type v;   // Works!!
  }; 
}
+1
source

All Articles