下面的代码显示了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 :(得分:3)
这是报告here的编译器错误。解决方法似乎是:
感谢您提交此反馈。虽然我们认识到这是一个有效的编译器错误,但它在产品周期的这一点上低于我们的分类栏。解决方法是定义您声明它的模板函数。如果您担心为每个翻译单元重新编译模板函数的性能影响,使用PCH文件应该可以消除这种开销。
谢谢, 马克罗伯茨 Visual C ++团队