我附上(同样,我们发现错误的第一篇文章)以下代码。
它不能用 VS2012 编译:
struct POD1
{
int x;
};
struct POD2
{
char x;
};
class A
{
public:
typedef POD1 innerType;
void doSomthing(POD1 t);
};
class B
{
public:
typedef POD2 innerType;
void doSomthing(POD2 t) { }
};
template<typename T>
class X
{
public:
using myType = typename T::innerType; // Doesn't work
void foo(myType bar) {} // Doesn't work
};
int _tmain(int argc, _TCHAR* argv[])
{
X<B> x;
POD2 b;
x.foo(b);
return 0;
}
我得到的错误:
1>c:\users\user\documents\visual studio 2012\projects\try\try\try.cpp(36): error C2873: 'myType' : symbol cannot be used in a using-declaration
1> c:\users\user\documents\visual studio 2012\projects\try\try\try.cpp(38) : see reference to class template instantiation 'X<T>' being compiled
1>c:\users\user\documents\visual studio 2012\projects\try\try\try.cpp(36): error C2143: syntax error : missing ';' before '='
1>c:\users\user\documents\visual studio 2012\projects\try\try\try.cpp(36): error C2238: unexpected token(s) preceding ';'
1>c:\users\user\documents\visual studio 2012\projects\try\try\try.cpp(37): error C2061: syntax error : identifier 'myType'
编辑 模板别名不适用于 VS2012(目前)。 有什么解决方法吗?
家伙
最佳答案
如果你看here你会看到别名模板在 VS2012 中不受支持。所以目前还不能使用该功能。
关于c++ - 访问模板内部 typedef,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17592293/