Search code examples
c++gccc++11aliasusing

C++ accessing nested type using alias (using vs typedef)


Today we have found a confusing C++11 alias-declaration behavior. Here is the example:

template<typename T>
struct Q
{
    typedef T t;
};

template<typename T>
void foo(Q<T> q)
{
    using q_t = Q<T>; 
    //typedef Q<T> q_t; // if we uncomment this and comment 'using' the example compiles
    typename q_t::t qwe; // <<<<<<<<<< Error: no type named ‘t’ in ‘using q_t = struct Q<T>’
}

int main(int argc, char *argv[])
{
    Q<int> q;
    foo(q);
    return 0;
}

The ISO 14882 (C++11) says that these two declarations must have the same semantics (page 145).

However if we have q_t declared with 'using' the example does not compile using GCC 4.7.2 (Debian Wheezy) and GCC 4.7.3 (Ubuntu 13.04) but the replacement of 'using' statement with 'typedef' statement makes it compiled.

Is it a bug in GCC or we just misunderstood the standards?


Solution

  • This seems to be a GCC 4.7 bug.

    Here is my test to compile your code, and it works using gcc 4.8.1

    So as the spec says this:

    using q_t = Q<T>;
    // is equivalent to this 
    typedef Q<T> q_t;