Namespaces
Variants

C++ attribute: deprecated (since C++14)

From cppreference.net
C++ language
General topics
Flow control
Conditional execution statements
Iteration statements (loops)
Jump statements
Functions
Function declaration
Lambda function expression
inline specifier
Dynamic exception specifications ( until C++17* )
noexcept specifier (C++11)
Exceptions
Namespaces
Types
Specifiers
constexpr (C++11)
consteval (C++20)
constinit (C++20)
Storage duration specifiers
Initialization
Expressions
Alternative representations
Literals
Boolean - Integer - Floating-point
Character - String - nullptr (C++11)
User-defined (C++11)
Utilities
Attributes (C++11)
Types
typedef declaration
Type alias declaration (C++11)
Casts
Memory allocation
Classes
Class-specific function properties
Special member functions
Templates
Miscellaneous

表示使用此属性声明的名称或实体已被 弃用 ,即虽然仍允许使用,但出于某些原因不推荐继续使用。

目录

语法

[ [ deprecated ] ] (1)
[ [ deprecated ( 字符串字面量 ) ] ] (2)
string-literal - 一个 未求值字符串字面量 ,可用于说明弃用的理由和/或建议替代实体

说明

表示允许使用以此属性声明的名称或实体,但出于某些原因不鼓励使用。编译器通常会对这类使用发出警告。如果指定了 字符串字面量 ,通常会将其包含在警告信息中。

此属性允许在以下名称或实体的声明中使用:

  • [ [ deprecated ] ] typedef S * PS ;
  • using PS [ [ deprecated ] ] = S * ;
(注:根据要求,HTML标签、代码块内容及C++术语均保持原样,仅对自然语言部分进行翻译。由于原文中除代码外无其他自然语言文本,故输出内容与原文完全一致)
  • (非成员)变量,例如: [ [ deprecated ] ] int x ;
  • 静态数据成员 ,例如: struct S { [ [ deprecated ] ] static constexpr char CR { 13 } ; } ;
  • 非静态数据成员 ,例如: union U { [ [ deprecated ] ] int n ; } ;
  • 函数 ,例如: [ [ deprecated ] ] void f ( ) ;
  • 命名空间 ,例如: namespace [ [ deprecated ] ] NS { int x ; }
  • 枚举 ,例如: enum [ [ deprecated ] ] E { } ;
  • 枚举项,例如 enum { A [ [ deprecated ] ] , B [ [ deprecated ] ] = 42 } ; ,
(C++17 起)
  • 模板特化 ,例如 template <> struct [ [ deprecated ] ] X < int > { } ;

被声明为非弃用的名称可以重新声明为弃用。被声明为弃用的名称无法通过不带此属性的重新声明来取消弃用状态。

示例

#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();
}

可能的输出:

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.

参考文献

  • C++23 标准 (ISO/IEC 14882:2024):
  • 9.12.5 弃用属性 [dcl.attr.deprecated]
  • C++20 标准 (ISO/IEC 14882:2020):
  • 9.12.4 弃用属性 [dcl.attr.deprecated]
  • C++17 标准 (ISO/IEC 14882:2017):
  • 10.6.4 弃用属性 [dcl.attr.deprecated]
  • C++14 标准 (ISO/IEC 14882:2014):
  • 7.6.5 弃用属性 [dcl.attr.deprecated]

参见

C 文档 关于 deprecated