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

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

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

相关文章:

c++ - 等同于在 C++ 中重新解释 Node.js fii

c++ - 在Mac上的Qt-隐藏主窗口时,应用停止响应停靠菜单图标的上下文菜单中的操作

c++ - 带有两个 dwDesiredAccess 的 OpenProcess 函数

C++删除操作相关查询

c++ - Bitmask - SPOJ LINEUP 错误答案

c# - 任何人都可以帮助使用 Kinect 和 C# 解决 "newSensor_AllFramesReady does not exist in current context"问题吗?

具有数字范围的 C# 变量或数组(例如 1 - 100)

c++ - Winsock 连接()错误 10051

C++模板和重载运算符

c++ - getValue 错误