Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

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:
[[deprecated]]
[[deprecated("string-literal")]]
  • 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.
[[deprecated("Use compute_v2() instead")]] 
void compute_v1();

class Processor {
public:
    [[deprecated]] void legacy_process();
};
2. Classes, Structs, and Unions Placed immediately after the class, struct, or union keyword, before the identifier.
class [[deprecated]] LegacyDataContainer {
    // ...
};
3. Variables and Non-static Data Members Placed at the beginning of the variable declaration.
[[deprecated]] int global_state_flag;

struct Config {
    [[deprecated("Redundant in v2.0")]] bool use_old_routing;
};
4. Enumerations and Enumerators Can be applied to the entire enumeration type or to individual enumerators (C++17 onwards for enumerators).
enum class [[deprecated]] OldState {
    Active,
    Inactive
};

enum class NewState {
    Pending,
    Running,
    Finished [[deprecated("Use Completed instead")]]
};
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.
[[deprecated]] typedef int Identifier;
using ID [[deprecated]] = int;
6. Namespaces (C++17) Placed immediately after the namespace keyword.
namespace [[deprecated("Namespace v1 is deprecated")]] v1 {
    void do_work();
}
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.
template <typename T>
class Container {};

template <>
class [[deprecated]] Container<bool> {};

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.
Master C++ with Deep Grasping Methodology!Learn More