G ++ - 6 errors on shaded template options, while g ++ - 5 not

Take the following example:

#include <vector> template <typename T, template <class T> class Container> std::vector<T> To_Vector(Container<T> const& c){ return std::vector<T>( c.begin(), c.end() ); } int main(){} 

With g++-5 it compiles without errors:

 g++-5 -o main main.cpp 

With g++-6 not possible to compile:

 g++-6 -o main main.cpp main.cpp:4:33: error: declaration of template parameter 'T' shadows template parameter template <typename T, template <class T> class Container> ^~~~~ main.cpp:4:11: note: template parameter 'T' declared here template <typename T, template <class T> class Container> 

Is the compiler wrong? Is my code wrong?
Why is g++-5 compiling this code, but g++-6 not?


 g++-5 --version g++-5 (Ubuntu 5.4.1-2ubuntu1~14.04) 5.4.1 20160904 Copyright (C) 2015 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 

 g++-6 --version g++-6 (Ubuntu 6.2.0-3ubuntu11~14.04) 6.2.0 20160901 Copyright (C) 2016 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
+6
source share
1 answer

The behavior of g ++ 6 is true, because according to the standard:

The template parameter should not be updated within its scope (including nested areas). The template parameter must not have the same name as the template name.

Region T begins immediately after its declaration, so it extends to the next template template template, which redefines T as a template parameter, therefore this rule is violated.

I assume that the change between g ++ 5 and g ++ 6 was due to fixing some bug report around a similar problem.

+4
source

All Articles