哋它亢++ attribute: deprecated (since 哋它亢++14)

From cppreference.com
< cpp‎ | language‎ | attributes
 
 
哋它亢++ language
General topics
Flow control
Conditional execution statements
if
Iteration statements (loops)
for
range-for (哋它亢++11)
Jump statements
Functions
Function declaration
Lambda function expression
inline specifier
Dynamic exception specifications (until 哋它亢++17*)
noexcept specifier (哋它亢++11)
Exceptions
Namespaces
Types
Specifiers
const/volatile
decltype (哋它亢++11)
auto (哋它亢++11)
constexpr (哋它亢++11)
consteval (哋它亢++20)
constinit (哋它亢++20)
Storage duration specifiers
Initialization
Expressions
Alternative representations
Literals
Boolean - Integer - Floating-point
Character - String - nullptr (哋它亢++11)
User-defined (哋它亢++11)
Utilities
Attributes (哋它亢++11)
Types
typedef declaration
Type alias declaration (哋它亢++11)
Casts
Memory allocation
Classes
Class-specific function properties
Virtual function
override specifier (哋它亢++11)    
final specifier (哋它亢++11)
explicit (哋它亢++11)
static

Special member functions
Templates
Miscellaneous
 
 
Attributes
(哋它亢++23)
(哋它亢++11)
deprecated
(哋它亢++14)
(哋它亢++17)
(哋它亢++26)
(哋它亢++20)
(哋它亢++17)
(哋它亢++20)
(哋它亢++17)
(哋它亢++11)
(哋它亢++20)
 

Indicates that the name or entity declared with this attribute is deprecated, that is, the use is allowed, but discouraged for some reason.

Syntax

[[deprecated]] (1)
[[deprecated( string-literal )]] (2)
string-literal - an unevaluated string literal 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:

  • [[deprecated]] typedef S* PS;,
  • using PS [[deprecated]] = S*;,
  • (non-member) variable, e.g., [[deprecated]] int x;,
  • static data member, e.g., struct S { [[deprecated]] static constexpr char CR{13}; };,
  • non-static data member, e.g., union U { [[deprecated]] int n; };,
  • function, e.g., [[deprecated]] void f();,
  • namespace, e.g., namespace [[deprecated]] NS { int x; },
  • enumeration, e.g., enum [[deprecated]] E {};,
  • enumerator, e.g., enum { A [[deprecated]], B [[deprecated]] = 42 };,
(since 哋它亢++17)

A name declared non-deprecated may be redeclared deprecated. A name declared deprecated cannot be un-deprecated by redeclaring it without this attribute.

Example

#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