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
[[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
}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.")]]- Can mark: functions, classes, variables, enums, using aliases
- Message string is optional but recommended
- Deprecated does not mean removed — the compiler still generates code
d2x checker deprecated-attribute