Difference between revisions of "cpp/language/attributes/deprecated"
From cppreference.com
< cpp | language | attributes
m (→Syntax: fmt) |
Andreas Krug (Talk | contribs) m (;, fmt) |
||
Line 15: | Line 15: | ||
{{par begin}} | {{par begin}} | ||
− | {{par | {{spar|string-literal}} | text that could be used to explain the rationale for deprecation and/or to suggest a replacing entity }} | + | {{par|{{spar|string-literal}}|text that could be used to explain the rationale for deprecation and/or to suggest a replacing entity}} |
{{par end}} | {{par end}} | ||
Line 28: | Line 28: | ||
* [[cpp/language/data_members|non-static data member]]: {{c|union U { [[deprecated]] int n; };}}, | * [[cpp/language/data_members|non-static data member]]: {{c|union U { [[deprecated]] int n; };}}, | ||
* [[cpp/language/function|function]]: {{c|[[deprecated]] void f();}}, | * [[cpp/language/function|function]]: {{c|[[deprecated]] void f();}}, | ||
− | * [[cpp/language/namespace|namespace]]: {{c|namespace [[deprecated]] NS { int x; } }} | + | * [[cpp/language/namespace|namespace]]: {{c|namespace [[deprecated]] NS { int x; } }}, |
* [[cpp/language/enum|enumeration]]: {{c|enum [[deprecated]] E {};}}, | * [[cpp/language/enum|enumeration]]: {{c|enum [[deprecated]] E {};}}, | ||
− | * enumerator: {{c|1=enum { A [[deprecated]], B [[deprecated]] = 42 };}} | + | * enumerator: {{c|1=enum { A [[deprecated]], B [[deprecated]] = 42 };}}, {{mark since c++17}} |
− | * [[cpp/language/template_specialization|template specialization]]: {{c|template<> struct [[deprecated]] X<int> {};}} | + | * [[cpp/language/template_specialization|template specialization]]: {{c|template<> struct [[deprecated]] X<int> {};}}. |
A name declared non-deprecated may be redeclared deprecated. A name declared deprecated cannot be un-deprecated by redeclaring it without this attribute. | A name declared non-deprecated may be redeclared deprecated. A name declared deprecated cannot be un-deprecated by redeclaring it without this attribute. | ||
Line 41: | Line 41: | ||
[[deprecated]] | [[deprecated]] | ||
− | void TriassicPeriod() { | + | void TriassicPeriod() |
+ | { | ||
std::clog << "Triassic Period: [251.9 - 208.5] million years ago.\n"; | std::clog << "Triassic Period: [251.9 - 208.5] million years ago.\n"; | ||
} | } | ||
[[deprecated("Use NeogenePeriod() instead.")]] | [[deprecated("Use NeogenePeriod() instead.")]] | ||
− | void JurassicPeriod() { | + | void JurassicPeriod() |
+ | { | ||
std::clog << "Jurassic Period: [201.3 - 152.1] million years ago.\n"; | std::clog << "Jurassic Period: [201.3 - 152.1] million years ago.\n"; | ||
} | } | ||
[[deprecated("Use calcSomethingDifferently(int).")]] | [[deprecated("Use calcSomethingDifferently(int).")]] | ||
− | int calcSomething(int x) { | + | int calcSomething(int x) |
+ | { | ||
return x * 2; | return x * 2; | ||
} | } | ||
Line 60: | Line 63: | ||
JurassicPeriod(); | JurassicPeriod(); | ||
} | } | ||
− | | p=true | + | |p=true |
− | | output= | + | |output= |
Triassic Period: [251.9 - 208.5] million years ago. | Triassic Period: [251.9 - 208.5] million years ago. | ||
Jurassic Period: [201.3 - 152.1] million years ago. | Jurassic Period: [201.3 - 152.1] million years ago. | ||
Line 82: | Line 85: | ||
===See also=== | ===See also=== | ||
{{dsc begin}} | {{dsc begin}} | ||
− | {{dsc see c | c/language/attributes/deprecated}} | + | {{dsc see c|c/language/attributes/deprecated}} |
{{dsc end}} | {{dsc end}} | ||
{{langlinks|es|ja|ru|zh}} | {{langlinks|es|ja|ru|zh}} |
Revision as of 07:23, 20 May 2023
Indicates that the name or entity declared with this attribute is deprecated, that is, the use is allowed, but discouraged for some reason.
Contents |
Syntax
[[deprecated]]
|
(1) | ||||||||
[[deprecated( string-literal )]]
|
(2) | ||||||||
string-literal | - | text that could be used to explain the rationale for deprecation and/or to suggest a replacing entity |
Explanation
Indicates that the use of the name or entity declared with this attribute is allowed, but discouraged for some reason. Compilers typically issue warnings on such uses. The string-literal, if specified, is usually included in the warnings.
This attribute is allowed in declarations of the following names or entities:
- class/struct/union: struct [[deprecated]] S;,
- typedef-name, including those declared by alias declaration: [[deprecated]] typedef S* PS;, using PS [[deprecated]] = S*;,
- variable, including static data member: [[deprecated]] int x;,
- non-static data member: union U { [[deprecated]] int n; };,
- function: [[deprecated]] void f();,
- namespace: namespace [[deprecated]] NS { int x; },
- enumeration: enum [[deprecated]] E {};,
- enumerator: enum { A [[deprecated]], B [[deprecated]] = 42 };, (since C++17)
- template specialization: template<> struct [[deprecated]] X<int> {};.
A name declared non-deprecated may be redeclared deprecated. A name declared deprecated cannot be un-deprecated by redeclaring it without this attribute.
Example
Run this code
#include <iostream> [[deprecated]] void TriassicPeriod() { std::clog << "Triassic Period: [251.9 - 208.5] million years ago.\n"; } [[deprecated("Use NeogenePeriod() instead.")]] void JurassicPeriod() { std::clog << "Jurassic Period: [201.3 - 152.1] million years ago.\n"; } [[deprecated("Use calcSomethingDifferently(int).")]] int calcSomething(int x) { return x * 2; } int main() { TriassicPeriod(); JurassicPeriod(); }
Possible output:
Triassic Period: [251.9 - 208.5] million years ago. Jurassic Period: [201.3 - 152.1] million years ago. main.cpp:20:5: warning: 'TriassicPeriod' is deprecated [-Wdeprecated-declarations] TriassicPeriod(); ^ main.cpp:3:3: note: 'TriassicPeriod' has been explicitly marked deprecated here [[deprecated]] ^ main.cpp:21:5: warning: 'JurassicPeriod' is deprecated: Use NeogenePeriod() instead [-Wdeprecated-declarations] JurassicPeriod(); ^ main.cpp:8:3: note: 'JurassicPeriod' has been explicitly marked deprecated here [[deprecated("Use NeogenePeriod() instead")]] ^ 2 warnings generated.
See also
C documentation for deprecated
|