模板中有两个默认参数,这里有什么问题?

Hal*_*alt 5 c++ templates default visual-studio-2010

下面的代码显示了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);
}
Run Code Online (Sandbox Code Playgroud)

dir*_*tly 3

这是此处报告的编译器错误。解决方法似乎是:

感谢您提交此反馈。虽然我们认识到这是一个有效的编译器错误,但在产品周期的此时,它低于我们的分类标准。解决方法是在声明模板函数的位置定义它。如果您担心为每个翻译单元重新编译模板函数对性能的影响,那么使用 PCH 文件应该可以消除这种开销。

谢谢 Mark Roberts Visual C++ 团队