【问题标题】:Two default parameters in template, what is wrong here?模板中有两个默认参数,这里有什么问题?
【发布时间】:2012-06-12 19:25:15
【问题描述】:

下面的代码显示了 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);
}

【问题讨论】:

  • 我已编辑您的帖子以提供main()。否则很好的问题,+1。

标签: c++ visual-studio-2010 templates default


【解决方案1】:

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

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

谢谢, 马克·罗伯茨 Visual C++ 团队

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-14
    • 1970-01-01
    • 2017-03-26
    相关资源
    最近更新 更多