To declare an object as deprecated in C/C++ under Visual Studio, you have three solutions:
#pragma deprecated(X)__pragma(deprecated(X))__declspec(deprecated(X))The first two ones are the same, except that only the second one can be used inside a macro; I've put the first one only for completeness. The third one seems to be the most used in the dev community.
I wonder what is the difference between the last two ones. According to the documentation on MSDN here and here, I understand that there is no difference. What is strange in that case is that a different warning code is raised depending on what you're using: C4995 for the pragma-case, C4996 for the declspec-case.
So does somebody knows if there is actually a difference (any tiny one), or why these directives don't raise the same warning code?
Deprecated attribute in C++14 with Examples Deprecated means the use of the name or entity declared with this attribute is allowed but discouraged for some reason.
See deprecated (C++):
(Microsoft specific) 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.
 
For #pragma vs. __pragma, see Pragma Directives and the __Pragma Keyword:
The __pragma() Keyword
Microsoft specific
The compiler also supports the __pragma keyword, which has the same functionality as the #pragma directive, but can be used inline in a macro definition.
It makes sense to note, as @Deduplicator mentioned, that C++14 introduces the [[deprecated]] attribute.
7.6.5 Deprecated attribute [dcl.attr.deprecated]
The attribute-token
deprecatedcan be used to mark names and entities whose use is still allowed, but is discouraged for some reason. [ Note: in particular,deprecatedis appropriate for names and entities that are deemed obsolescent or unsafe. —end note ]
One more thing I just found out.
I have this class defined in a header file:
class X
{
   void F1();
   void F2();
}
Now, I want to deprecate F1, but when you use the pragma deprecated I get the warning every time the header file is included, even if F1 is never used.
class X
{
#pragma deprecated(F1)
   void F1();
   void F2();
}
Now change it to using the __declspec(deprecated()) and you only get the deprecated message when and exactly where F1 is used. In my opinion you should never use #pragma deprecated unless you want people to use #pragma warning (disable: 4995) as it is pretty nasty to have warnings that you can't get rid of.
class X
{
   __declspec(deprecated("Please use F2")) void F1();
   void F2();
}
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With