Search code examples
c++templatesc++11using

Access template inner typedef


I attach (again, the first post we buggy) the following code.

It doesn't compile with 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;
}

The errors I get:

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'

EDIT template aliasing doesn't work (yet) with VS2012. Any workarounds?

Guy


Solution

  • Simple typedef should work:

    template<typename T>
    class X
    {
    public:
        typedef typename T::innerType myType;
        void foo(myType  bar) {}
    };