2012-06-12 56 views
5

下面的代碼顯示了2個Foo模板,每個模板都帶有2個默認參數,Foo1有一個單獨的原型,而Foo2沒有,否則它們是相同的。模板中有兩個默認參數,這裏有什麼問題?

爲什麼第一次調用Foo1會導致編譯器(VS2010 Native C++)在其他3個工作時產生錯誤?

#include <limits> 

// not needed but to prevent answers in this direction... 
#undef max 
#undef min 

template< typename T > 
void Foo1(T v1 = std::numeric_limits<T>::min(), T v2 = std::numeric_limits<T>::max()); 

template< typename T > 
inline 
void Foo1(T v1, T v2) 
{ 
    // ... 
} 

template< typename T > 
inline 
void Foo2(T v1 = std::numeric_limits<T>::min(), T v2 = std::numeric_limits<T>::max()) 
{ 
    // ... 
} 

int main() 
{ 
    Foo1<int>(0); /* Will cause error C2589: '::' : illegal token on right side of '::' */ 
    Foo1<int>(0, 10); 
    Foo2<int>(0); 
    Foo2<int>(0, 10); 
} 
+0

我已經編輯您的文章,以提供一個'主()'。否則很好的問題,+1。 –

回答

3

這是一個編譯器bug,報告爲here。解決方法似乎是:

感謝您提交此反饋。雖然我們認識到這是一個有效的編譯器錯誤,但在產品週期的這一點,它低於我們的分診欄。解決方法是定義你聲明它的模板函數。如果您擔心重新編譯每個翻譯單元的模板函數的性能影響,使用PCH文件應該消除這種開銷。

謝謝, 馬克羅伯茨 的Visual C++團隊

+0

Thx爲這個快速和非常重要的答案。 – Halt