2011-05-06 83 views
1

我遇到模板類專業化問題,請參閱下面的代碼。C++,模板專業化問題

template <typename T> 
class Point 
{ 
    private 
      T x, y; 
      typedef T Type; 

    public: 

      Point (const T & x_, const T & y_) : x (x_), y (y_) {} 
}; 

template <typename Item> 
struct TItems 
{ 
    typedef std::vector <Item> Type; 
}; 


template <typename Item> 
class Container 
{ 
    protected: 
      typename TItems <Item>::Type items; 

    public: 
      typedef Item type; 
}; 

是否有可能專門爲Point的Container類?

更新的問題:

我試過下面的代碼,它有效嗎?

template <typename T> 
class Container < Point <T> > 
{ 

}; 

int _tmain(int argc, _TCHAR* argv[]) 
{ 
return 0; 

Container <Point <double> > points; 
} 

回答

1

你可以,是的,但你的語法是不完全正確。既然這樣,編譯器不知道什麼T是,所以你要告訴它,它是一個模板參數:

template<typename T> 
class Container<Point<T> > { }; 
+0

謝謝,我在與您發佈答案的同時糾正了我的代碼:-) – Johnas 2011-05-06 21:02:18

1

是的,你可以專門化你的班級與該類型Point <T>

編輯:

我嘗試下面的代碼,它是有效的 ?

如果你已經試過下面的代碼,你不知道它是否編譯? 0_o

int _tmain(int argc, _TCHAR* argv[]) 
{ 
Container <Point <double> > points; 
return 0; // return should be here nor program will exit before creating Container <Point <double> > points; 
} 

[R

+0

'_tmain'是不是一個合法的函數名,因此,單憑它編譯對代碼是否合法沒有真正的幫助。 – 2011-05-06 21:06:10

+0

我認爲他正在使用視覺工作室。 – 2011-05-06 21:07:56

+0

@Dennis,'_tmain'不是一個合法的函數名稱?這是一個有效的標識符,甚至不是保留的標識符。 – 2011-05-06 21:26:26