How can I mark a C++ class method as deprecated?

c++, deprecated, portability

Solution

In C++14, you can mark a function as deprecated using the `[[deprecated]]` attribute (see section 7.6.5 [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.

For example, the following function `foo` is deprecated:

[[deprecated]]
void foo(int);

It is possible to provide a message that describes why the name or entity was deprecated:

[[deprecated("Replaced by bar, which has an improved interface")]]
void foo(int);

The message must be a string literal.

For further details, see “Marking as deprecated in C++14”.

Problem

I have a method in a C++ interface that I want to deprecate, with portable code. When I Googled for this all I got was a Microsoft specific solution; `#pragma deprecated` and `__declspec(deprecated)`. If a general or fully-portable deprecation solution is not available, I will accept as a "second prize solution" one that can be used multiple specific compilers, like MSVC and a GCC.

Original source