下面的代码显示了 2 个 Foo 模板,每个模板都有 2 个默认参数,Foo1 有一个单独的原型(prototype)而 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);
}
最佳答案
这是报告的编译器错误 here .解决方法似乎是:
Thank you for submitting this feedback. While we recognize that this is a valid compiler bug, it is below our triage bar at this point in the product cycle. The workaround is to define the template function where you have declared it. If you are concerned about the perf impact of recompiling the template function for each translation unit, using PCH files should eliminate this overhead.
Thanks, Mark Roberts Visual C++ Team
关于c++ - 模板中有两个默认参数,这里有什么问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10994498/