c++ - 访问模板内部 typedef

标签 c++ templates c++11 using

我附上(同样,我们发现错误的第一篇文章)以下代码。

它不能用 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/

相关文章:

c++ - 根据鼠标选择Opengl拖动对象

c# - 如何为新的 C# 类/接口(interface)编辑 Visual Studio 模板?

c++ - 在编译时检查模板参数类型是否为集合或多重集,以及容器的元素类型是否为算术

c++ - C++中的模板继承错误

algorithm - 为什么 <algorithm> 函数中的 std::set 将非 const 对象更改为 const?

c++ - 在 C 程序中使用 C++ 代码,反之亦然

c++ - 编译时如何禁用部分代码

c++ - 尝试根据另一个 vector 对一个 vector 进行排序时出现不一致

java - 如何设置JAssimp?

c++ - C++0x什么时候发布,什么时候成为事实上的标准?