编译器警告(等级 3)C4996

“function”: 被声明为否决的

编译器遇到了标记有 deprecated 的函数。 在未来版本中可能不再支持此函数。 可以用 warning 杂注关闭此警告(如下例所示)。

为在其中声明了函数的行和在其中使用了函数的行生成 C4996。

如果您在 std 命名空间中使用 <hash_map> 和 <hash_set> 头文件的成员,将会显示 C4996。 有关更多信息,请参见 stdext 命名空间

为了支持新的、更安全的函数,否决了某些 CRT 和标准 C++ 库函数。 有关不推荐使用的函数的更多信息,请参见 Security Features in the CRTSafe Libraries: Standard C++ Library

如果出于安全原因使用了否决的 MFC 或 ATL 函数,也可能发生 C4996。 若要取消这些警告,请参见 _AFX_SECURE_NO_WARNINGS_ATL_SECURE_NO_WARNINGS

在使用封送处理库时也可能会发生 C4996。 在这种情况下,C4996 是错误,而非警告。 使用 marshal_as 在需要 marshal_context Class的两种数据类型之间进行转换时,将发生此错误。 如果封送处理库不支持转换,您也会收到此错误。 有关封送处理库的更多信息,请参见 Overview of Marshaling in C++

示例

下面的示例生成 C4996。

// C4996.cpp
// compile with: /W3
// C4996 warning expected
#include <stdio.h>

// #pragma warning(disable : 4996)
void func1(void) {
   printf_s("\nIn func1");
}

__declspec(deprecated) void func1(int) {
   printf_s("\nIn func2");
}

int main() {
   func1();
   func1(1);
}

如果用 _SECURE_SCL 1 进行编译时未使用选中的迭代器,也可能发生 C4996。 有关更多信息,请参见Checked Iterators

下面的示例生成 C4996。

// C4996_b.cpp
// compile with: /EHsc /W3 /c
#define _SECURE_SCL 1
#include <algorithm>
using namespace std;
using namespace stdext;
int main() {
   int a [] = {1, 2, 3};
   int b [] = {10, 11, 12};
   copy(a, a + 3, b + 1);   // C4996
// try the following line instead
//   copy(a, a + 3, b);
   copy(a, a + 3, checked_array_iterator<int *>(b, 3));   // OK
}

下面的示例会生成 C4996,因为封送处理库需要上下文才能从 System::String 转换为 const char *。

// C4996_Marshal.cpp
// compile with: /clr 
// C4996 expected
#include <stdlib.h>
#include <string.h>
#include <msclr\marshal.h>

using namespace System;
using namespace msclr::interop;

int main() {
   String^ message = gcnew String("Test String to Marshal");
   const char* result;
   result = marshal_as<const char*>( message );
   return 0;
}