编译器警告 C5037

“member-function”:类模板成员的外部定义不能有默认参数

注解

不允许对模板类中成员函数的外部定义使用默认自变量。 编译器在 /permissive 下发出 3 级警告,在 /permissive- 下发出错误。

Visual Studio 2017 版本 15.3 中引入了此警告。 在 Visual Studio 2017 版本 15.3 之前的编译器版本中编译时没有警告的代码现在可能生成 C5037。 若要了解如何禁用特定编译器版本或更高版本中引入的警告,请参阅由编译器版本引发的编译器警告。 可以使用 /wd:5037 编译器选项或 #pragma warning(disable:5037) 来禁用它。

示例

在以前版本的 Visual Studio 中,以下格式错误的代码可能会导致发生运行时故障。 Visual Studio 2017 版本 15.3 生成警告 C5037:

// C5037.cpp
// compile using: cl /c /permissive- C5037.cpp
template <typename T>
struct A {
    T f(T t, bool b = false);
};

template <typename T>
T A<T>::f(T t, bool b = false) // C5037: 'A<T>::f': an out-of-line definition of a member of a class template cannot have default arguments
{
    // ...
}

若要修复此错误,请删除 = false 默认自变量。