Skip to content

Latest commit

 

History

History
73 lines (49 loc) · 2.57 KB

File metadata and controls

73 lines (49 loc) · 2.57 KB

🌎 中文 | English

deprecated Attribute

C++14 introduces the [[deprecated]] attribute to mark deprecated functions, classes, or variables, producing compile-time warnings

Book Video Code X
cppreference-attribute / markdown Video Explanation Exercise Code

Why introduced?

  • Before C++11, there was no standard way to mark deprecated APIs — only documentation or non-standard #warning
  • [[deprecated]] produces warnings at compile time that callers cannot ignore

I. Basic Usage and Scenarios

[[deprecated("Use new_api() instead")]]
void old_api() { }

[[deprecated]]
int legacy_value = 42;

void modern_code() {
    old_api();       // warning: old_api is deprecated
    int x = legacy_value;  // warning: legacy_value is deprecated
}

II. Real-World Case — [[deprecated]] in the STL

The MSVC STL wraps [[deprecated]] in macros for deprecation warnings on obsolete headers. The example below cites the vendored MSVC STL (source: msvc-stl/stl/inc/yvals_core.h)

// MSVC STL · msvc-stl/stl/inc/yvals_core.h (abridged)
#define _CXX17_DEPRECATE_C_HEADER \
    [[deprecated("warning STL4004: " \
                 "<ccomplex>, <cstdalign>, <cstdbool>, and <ctgmath> " \
                 "are deprecated in C++17.")]]

III. Notes

  • Can mark: functions, classes, variables, enums, using aliases
  • Message string is optional but recommended
  • Deprecated does not mean removed — the compiler still generates code

IV. Exercise Code

Exercise Topics

Auto-Checker Command

d2x checker deprecated-attribute

V. Other