Mark class / method deprecated or deprecated in C ++

Is there a way to mark methods / classes in C ++ as deprecated?

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.

+3
source share
4 answers

The easiest way is with #define DEPRECATED. In GCC, it expands to __attribute__((deprecated)), in Visual C ++, it expands to __declspec(deprecated), and in compilers that don't have something silymar, it expands to nothing.

+7
source

Only using compiler dependent pragmas: see the documentation

 int old_fn () __attribute__ ((deprecated));
+7
source

I do not know about the C ++ version that you are using, but Microsoft Visual C ++ has an outdated pragma . Your version may have something similar.

0
source

The C ++ 17 standard now provides this function: https://en.cppreference.com/w/cpp/language/attributes/deprecated

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

Source: https://habr.com/ru/post/1707893/


All Articles