2011-08-01 43 views
3

考慮這種過於簡單的測試:對C++模板的基本問題

class foo 
{ 
    public: 
     foo(int i); 
     template< typename T > foo(T); 
}; 

template<> foo::foo(int i) {} 

現在,GCC是幸福的編譯時接受這一點,但RVCT編譯器將發出錯誤:

test.cpp", line 11: Error: #792: "foo::foo(int)" is not an entity that can be explicitly specialized 
template<> foo::foo(int i) {} 

如無問題的「你爲什麼要這麼做」,這是合法的C++(從學術角度?)提前

感謝

+0

只是爲了讓你知道 - 編譯+在VS2010上運行良好 – Schnommus

+0

它適用於我。使用g ++和VC++。 –

回答

3

你正試圖做一個明確的專門化template<typename T> foo(T)其中T = int。

你真的想要這個嗎?

template<typename T> foo::foo(T) { 
} 

---編輯---

只是要清楚:「明確的專業化」,是用C++合法,但顯然你的編譯器不支持它(個別方法,無論如何,也許它確實在整個班上?)。

+0

不要以爲這是個問題。 – Schnommus

+0

他在編譯器上顯示明確的特化錯誤,顯然不支持顯式特化。我想我回答「這是合法的」和「爲什麼我會得到這個錯誤」。 –

+1

你說這是合法的C++? – Schnommus