在MSVC2010中给出如下代码:
错误 C2039:“my_type”:不是“全局命名空间”的成员
template<typename T>
class C
{
public:
typedef T my_type;
};
C<int> c;
auto f = [&c]() {
decltype(c)::my_type v2; // ERROR C2039
};
我找到了一个蹩脚的方法来解决它,但我想知道当你只有一个对象实例时获取 typedef 的正确方法是什么。
最佳答案
从一堆非常有用的评论中,我得到了一个可行的解决方案。感谢大家。 remove_reference 作为身份对象具有双重用途。
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!!
};
}
关于c++ - 如何从对象实例中获取成员 typedef,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30199241/