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

Today we discovered the confusing behavior of declaring a C ++ 11 alias. Here is an 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; } 

ISO 14882 (C ++ 11) says that these two declarations should have the same semantics (p. 145).

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

Is this a mistake in the GCC or are we just misunderstood the standards?

+4
source share
2 answers

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

Since the specification says this:

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

Compiles for me with g ++ 4.8.1 using --std = C ++ 11

+1
source

Source: https://habr.com/ru/post/1500922/


All Articles