~/okanakyuz
← all posts

[[deprecated]]

[[deprecated]] is a C++14 attribute that marks a function, class or variable as something that will be removed, so the compiler warns anyone who uses it.

3 April 2020updated 25 November 20261 min read

Deprecated is an annotation (an attribute) which came into our life with C++14. You use it to mark a function or class that will be removed in the future. The code still compiles, but everybody who uses it gets a warning from the compiler.

[[deprecated]]
void old_function() {}

[[deprecated("use new_function() instead")]]
void older_function() {}

void new_function() {}

struct [[deprecated("use NewConfig")]] OldConfig {};

int main()
{
    old_function();    // warning: 'old_function' is deprecated
    older_function();  // warning: ... use new_function() instead
    OldConfig cfg;     // warning too
}

The second form with a message is the useful one. Tell the user what to use instead, otherwise they only know that something is wrong but not how to fix it.

You can put it on functions, classes, structs, unions, variables, static data members, typedefs/aliases, enums and template specializations. Since C++17 it also works on namespaces and on single enumerators:

enum class Color { Red, Green, Purple [[deprecated("use Red")]] };

Before C++14 every compiler had its own way, __attribute__((deprecated)) on GCC/Clang and __declspec(deprecated) on MSVC. You may still see them in old code. On GCC/Clang the warning is -Wdeprecated-declarations, on MSVC it is C4996. If you build with warnings as errors, keep in mind that a new [[deprecated]] in a library can break your build.