下面的代碼顯示了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);
}
我已經編輯您的文章,以提供一個'主()'。否則很好的問題,+1。 –