|
| 1 | +<div align=right> |
| 2 | + |
| 3 | + 🌎 [中文] | [English] |
| 4 | +</div> |
| 5 | + |
| 6 | +[中文]: ../../cpp14/07-deprecated-attribute.html |
| 7 | +[English]: ./07-deprecated-attribute.html |
| 8 | + |
| 9 | +# deprecated Attribute |
| 10 | + |
| 11 | +C++14 introduces the `[[deprecated]]` attribute to mark deprecated functions, classes, or variables, producing compile-time warnings |
| 12 | + |
| 13 | +| Book | Video | Code | X | |
| 14 | +| --- | --- | --- | --- | |
| 15 | +| [cppreference-attribute](https://en.cppreference.com/w/cpp/language/attributes) / [markdown](https://github.com/mcpp-community/d2mcpp/blob/main/book/en/src/cpp14/07-deprecated-attribute.md) | [Video Explanation]() | [Exercise Code](https://github.com/mcpp-community/d2mcpp/blob/main/dslings/en/cpp14/07-deprecated-attribute-0.cpp) | | |
| 16 | + |
| 17 | + |
| 18 | +**Why introduced?** |
| 19 | + |
| 20 | +- Before C++11, there was no standard way to mark deprecated APIs — only documentation or non-standard `#warning` |
| 21 | +- `[[deprecated]]` produces warnings at compile time that callers cannot ignore |
| 22 | + |
| 23 | +## I. Basic Usage and Scenarios |
| 24 | + |
| 25 | +```cpp |
| 26 | +[[deprecated("Use new_api() instead")]] |
| 27 | +void old_api() { } |
| 28 | + |
| 29 | +[[deprecated]] |
| 30 | +int legacy_value = 42; |
| 31 | + |
| 32 | +void modern_code() { |
| 33 | + old_api(); // warning: old_api is deprecated |
| 34 | + int x = legacy_value; // warning: legacy_value is deprecated |
| 35 | +} |
| 36 | +``` |
| 37 | + |
| 38 | +## II. Real-World Case — [[deprecated]] in the STL |
| 39 | + |
| 40 | +> The MSVC STL wraps `[[deprecated]]` in macros for deprecation warnings on obsolete headers. The example below cites the vendored [MSVC STL](https://github.com/mcpp-community/d2mcpp/tree/main/msvc-stl) (source: [`msvc-stl/stl/inc/yvals_core.h`](https://github.com/mcpp-community/d2mcpp/blob/main/msvc-stl/stl/inc/yvals_core.h#L1057-L1061)) |
| 41 | +
|
| 42 | +```cpp |
| 43 | +// MSVC STL · msvc-stl/stl/inc/yvals_core.h (abridged) |
| 44 | +#define _CXX17_DEPRECATE_C_HEADER \ |
| 45 | + [[deprecated("warning STL4004: " \ |
| 46 | + "<ccomplex>, <cstdalign>, <cstdbool>, and <ctgmath> " \ |
| 47 | + "are deprecated in C++17.")]] |
| 48 | +``` |
| 49 | +
|
| 50 | +## III. Notes |
| 51 | +
|
| 52 | +- Can mark: functions, classes, variables, enums, using aliases |
| 53 | +- Message string is optional but recommended |
| 54 | +- Deprecated does not mean removed — the compiler still generates code |
| 55 | +
|
| 56 | +## IV. Exercise Code |
| 57 | +
|
| 58 | +### Exercise Topics |
| 59 | +
|
| 60 | +- 0 - [[[deprecated]] Attribute — Marking Deprecated Functions](https://github.com/mcpp-community/d2mcpp/blob/main/dslings/en/cpp14/07-deprecated-attribute-0.cpp) |
| 61 | +
|
| 62 | +### Auto-Checker Command |
| 63 | +
|
| 64 | +``` |
| 65 | +d2x checker deprecated-attribute |
| 66 | +``` |
| 67 | + |
| 68 | +## V. Other |
| 69 | + |
| 70 | +- [Discussion Forum](https://forum.d2learn.org/category/20) |
| 71 | +- [d2mcpp Tutorial Repository](https://github.com/mcpp-community/d2mcpp) |
| 72 | +- [Tutorial Video List](https://space.bilibili.com/65858958/lists/5208246) |
| 73 | +- [Tutorial Support Tool - xlings](https://github.com/openxlings/xlings) |
0 commit comments