deprecated (C++)
This topic is about the Microsoft-specific deprecated declspec declaration. For information about the C++14 [[deprecated]]
attribute, and guidance on when to use that attribute vs. the Microsoft-specific declspec or pragma, see C++ Standard Attributes.
With the exceptions noted below, the deprecated
declaration offers the same functionality as the deprecated pragma:
The
deprecated
declaration lets you specify particular forms of function overloads as deprecated, whereas the pragma form applies to all overloaded forms of a function name.The
deprecated
declaration lets you specify a message that will display at compile time. The text of the message can be from a macro.Macros can only be marked as deprecated with the
deprecated
pragma.
If the compiler encounters the use of a deprecated identifier or the standard [[deprecated]]
attribute, a C4996 warning is thrown.
Examples
The following sample shows how to mark functions as deprecated, and how to specify a message that will be displayed at compile time, when the deprecated function is used.
// deprecated.cpp
// compile with: /W3
#define MY_TEXT "function is deprecated"
void func1(void) {}
__declspec(deprecated) void func1(int) {}
__declspec(deprecated("** this is a deprecated function **")) void func2(int) {}
__declspec(deprecated(MY_TEXT)) void func3(int) {}
int main() {
func1();
func1(1); // C4996
func2(1); // C4996
func3(1); // C4996
}
The following sample shows how to mark classes as deprecated, and how to specify a message that will be displayed at compile time, when the deprecated class is used.
// deprecate_class.cpp
// compile with: /W3
struct __declspec(deprecated) X {
void f(){}
};
struct __declspec(deprecated("** X2 is deprecated **")) X2 {
void f(){}
};
int main() {
X x; // C4996
X2 x2; // C4996
}