Skip to main content
The [[deprecated]] attribute is a standard C++14 feature used to mark an entity as obsolete or discouraged from future use while retaining it in the codebase for backward compatibility. When the compiler encounters a reference to an entity marked with this attribute, it emits a diagnostic warning during the compilation phase without halting the build process.

Syntax

The attribute can be applied in two forms within an attribute-specifier sequence:
  • Standard form: Marks the entity as deprecated. The compiler generates a default warning message.
  • Parameterized form: Accepts a narrow string literal. The compiler includes this string literal in the diagnostic output.

Applicability and Placement

The attribute can be applied to a wide variety of declarations. Its placement follows standard C++ attribute grammar rules, generally appearing at the beginning of a declaration or immediately after a class/enum key. Valid targets include: 1. Functions and Methods Placed at the beginning of the function declaration.
2. Classes, Structs, and Unions Placed immediately after the class, struct, or union keyword, before the identifier.
3. Variables and Non-static Data Members Placed at the beginning of the variable declaration.
4. Enumerations and Enumerators Can be applied to the entire enumeration type or to individual enumerators (C++17 onwards for enumerators).
5. Typedefs and Type Aliases For typedef declarations, the attribute is placed at the beginning of the declaration. For alias declarations (using), the attribute specifier sequence must be placed immediately after the identifier.
6. Namespaces (C++17) Placed immediately after the namespace keyword.
7. Template Specializations Placed after the template parameter list, typically immediately after the class/struct keyword. Standard C++ does not permit attributes to precede the template keyword.

Compiler Mechanics

  • Semantic Neutrality: The [[deprecated]] attribute does not alter the linkage, storage duration, memory layout, or runtime semantics of the entity. It is strictly a compile-time diagnostic tool.
  • Diagnostic Suppression: Compilers provide flags to suppress these specific diagnostics if necessary (e.g., -Wno-deprecated-declarations in GCC/Clang, or #pragma warning(disable : 4996) in MSVC).
  • Pre-C++14 Equivalents: Prior to C++14, this behavior was achieved using compiler-specific extensions such as __attribute__((deprecated)) (GCC/Clang) or __declspec(deprecated) (MSVC). The standard [[deprecated]] attribute unifies these under a single, cross-platform syntax.
Tired of Poor C++ Skills? Fix That With Deep Grasping!Learn More