Chris Andrews
Chris Andrews

Reputation: 1921

Mark class/method obsolete or deprecated in C++

Is there a way of marking methods/classes in C++ as obsolete?

In c# you can write:

[Obsolete("You shouldn't use this method anymore.")]
void foo() {}

I use the GNU toolchain/Eclipse CDT if that matters.

Upvotes: 7

Views: 7456

Answers (4)

Casey
Casey

Reputation: 10966

The c++14 standard (or later) now provides this feature: https://en.cppreference.com/w/cpp/language/attributes/deprecated

[[deprecated]] void F();
[[deprecated("reason")]] void G();
class [[deprecated]] H { /*...*/ };

Upvotes: 9

MSalters
MSalters

Reputation: 180235

The easiest way is with a #define DEPRECATED. On GCC, it expands to __attribute__((deprecated)), on Visual C++ it expands to __declspec(deprecated), and on compilers that do not have something silimar it expands to nothing.

Upvotes: 9

Matt Davis
Matt Davis

Reputation: 46052

I don't know about the version of C++ you are using, but Microsoft's Visual C++ has a deprecated pragma. Perhaps your version has something similar.

Upvotes: 0

dirkgently
dirkgently

Reputation: 111298

Only using compiler dependent pragmas: look up the documentation

 int old_fn () __attribute__ ((deprecated));

Upvotes: 7

Related Questions