2016-09-07 1 views
6

は、次の例を取ることはありません:g++-5グラム++ - 影のテンプレートパラメータに6エラーG ++ながら - 5

#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(){} 

を、それがエラーなしでコンパイル:g++-6

はコンパイルに失敗しています:

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> 

コンパイラが間違っていますか?私のコードは間違っていますか?
g++-6はなぜg++-5はこのコードをコンパイルしますか?


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. 
+0

@skypjackだから、ちょうどが私のために構築するので、私はに 'T'を残すべきです。 –

+0

これは有効かつ有効な解決策です。あなたの例では 'T'は役に立たないのですか? – skypjack

+0

@skypjackあなたが正しいです。コンテナの型名は型Tに特化しなければならないと考えるのは混乱していたに違いないが、実際のパラメータでは起こる。 –

答えて

4

グラム++ 6の動作は、標準に従ったので、正しい:

テンプレートパラメータは、その範囲内に再宣言してはなりません(入れ子スコープを含む)。 テンプレートパラメータは、テンプレート名と同じ名前であってはなりません。

Tの範囲が権利宣言後に始まるので、したがって、このルールに違反し、テンプレートパラメータとしてTを再宣言次のテンプレートのテンプレートパラメータに延びています。

g ++ 5とg ++ 6の間の変更は、同様の問題のバグレポートを修正したことによるものだと思います。

+0

なぜこれが代わりに機能しますか? 'template