2013-12-12 38 views
3

下面的代碼是在MVCC下編譯的,但不是g ++,我不知道爲什麼。使用模板參數的typedef不能用g ++工作

template <typename T> 
class A 
{ 
public: 
    template <typename C> 
    class B 
    { 
    public: 
    C* cptr; 
    }; 

    typedef typename A<T>::B<T> Ttype; 
}; 

int main(int argc,char** argv) 
{ 
    A<int>::Ttype d; 
    d.cptr = 0; 
} 

與G ++,你

error: 'typename A<T>::B name template<class T> template<class C> class A<T>::B', which is not a type 

我與編譯-std = C++ 11

+0

鏗鏘集編譯它,一定是GCC中的一個bug – uk4321

+0

哪個版本的gcc? 'gcc --version' – Till

+0

只需使用'typedef B Ttype;'解決問題。 –

回答

6

基於GCC的錯誤消息,問題是你要求A<T>::B是一種類型,但它不是:它一類模板。無論gccclang很高興與

typedef A<T>::B<T> Ttype; 

即刪除typename。在給定的背景下,不可能將B專門化爲與它顯然不同的東西。

using -alias不只是用不同的語法相同:

using Ttype = A<T>::B<T>; 

使用額外template關鍵字第一次指出B實際上是一個template,然後結合的typename的實例化的符號B<T>是一種類型:

typedef typename A<T>::template B<T> Ttype; 

using Ttype = typename A<T>::template B<T>; 

由於類模板B是本地的,無論如何,是不是真的需要在這方面的資格,即

typedef B<T> Ttype; 

using Ttype = B<T>; 

做的工作爲好。

+0

+1爲什麼這三個不同的答案工作!? – 0x499602D2

+0

@ 0x499602D2 LightnessRacesinOrbit的答案是使用類型別名,不同的方法。 – Till

+0

當然,'typedef B Ttype;'也被g ++ 4.8和clang ++ 3.4所接受 – dyp

6

我認爲這是與MVCC的錯誤。這不會編譯,因爲你需要使用template的歧義B爲模板:

typedef typename A<T>::template B<T> Ttype; 
+0

這對我也不起作用。 –

+0

@ uk4321它爲我編譯。 – 0x499602D2

+0

很好的答案。 MVCC(版本16.00.40219.01)不接受這個,只是爲了澄清MVCC錯誤。 – IdeaHat

5

看起來像我的錯誤。

在GCC 4.8.1以下工作:

using Ttype = A<T>::B<T>;