Difference between __pragma(deprecated) and __declspec(deprecated)

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 deprecated can be used to mark names and entities whose use is still allowed, but is discouraged for some reason. [ Note: in particular, deprecated is 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();
}

Tags:

Windows

C

Pragma