C++ attribute: deprecated (since C++14)
From cppreference.com
 
 
 < cpp | language | attributes 
 
 
 C++ 
 Feature test macros (C++20)
 Concepts library (C++20)
 Metaprogramming library (C++11)
 Ranges library (C++20)
 Filesystem library (C++17)
 Concurrency support library (C++11)
 Execution control library (C++26)
C++ language 
 
  
  
 General topics
 Conditional execution statements
 Iteration statements (loops)
 Jump statements
 Dynamic exception specifications (until C++17*)
noexcept specifier (C++11) Exceptions
 Namespaces
 Types
 Specifiers
 User-defined (C++11)
 Utilities
 Attributes (C++11)
 Types
 Type alias declaration (C++11)
 Casts
 Memory allocation
 Class-specific function properties
 Special member functions
 Miscellaneous
Declarations 
 
 
 
 
 
 
 
 
 
 Overview
 Specifiers
(C++11)
(C++20)
(C++20)
 Translation-unit-local (C++20)
(C++11)
(C++11)
(C++11)
 Pack indexing specifier (C++26)    
 Attributes (C++11)
 Declarators
 Block declarations
   →Structured binding declaration (C++17)
 Alias declaration (C++11)
 static_assert declaration (C++11)
 Opaque enum declaration (C++11)
 Other declarations
 Explicit template instantiation (C++11)
 Attribute declaration (C++11)
Attributes 
 
 
 
 
 
 
 
 
 
 
 
 
(C++23)
(C++11)(until C++26)
deprecated
(C++14)
(C++17)
(C++26)
(C++20)
(C++17)
(C++17)
(C++11)
(C++20)
(TM TS)
(C++20)
Indicates that the name or entity declared with this attribute is deprecated, that is, the use is allowed, but discouraged for some reason.
Contents
[edit] 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
[edit] 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, e.g., struct [[deprecated]] S;,
- typedef-name, including those declared by alias declaration, e.g.,
- [[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 };,
- template specialization, e.g., 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.
[edit] 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.[edit] References
- C++23 standard (ISO/IEC 14882:2024):
- 9.12.5 Deprecated attribute [dcl.attr.deprecated]
 
- C++20 standard (ISO/IEC 14882:2020):
- 9.12.4 Deprecated attribute [dcl.attr.deprecated]
 
- C++17 standard (ISO/IEC 14882:2017):
- 10.6.4 Deprecated attribute [dcl.attr.deprecated]
 
- C++14 standard (ISO/IEC 14882:2014):
- 7.6.5 Deprecated attribute [dcl.attr.deprecated]
 
[edit] See also
C documentation  for deprecated