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 (非推奨)であることを示します。つまり、使用は許可されていますが、何らかの理由で推奨されていません。

目次

翻訳のポイント: - 「Contents」→「目次」に翻訳 - HTMLタグ、属性、class名、id名はすべて保持 - リンクテキスト(Syntax, Explanation, Example, References, See also)はC++関連の専門用語として翻訳せず保持 - 数値、構造、フォーマットは完全に維持

構文

[ [ deprecated ] ] (1)
[ [ deprecated ( 文字列リテラル ) ] ] (2)
string-literal - 非評価文字列リテラルで、非推奨の根拠を説明したり、代替エンティティを提案するために使用できる

説明

この属性で宣言された名前またはエンティティの使用は許可されているが、何らかの理由で推奨されないことを示します。コンパイラは通常、そのような使用に対して警告を発します。 string-literal が指定されている場合、通常は警告に含まれます。

この属性は以下の名前またはエンティティの宣言で使用できます:

  • [ [ deprecated ] ] typedef S * PS ;
  • using PS [ [ deprecated ] ] = S * ;
  • 列挙子、例: enum { A [ [ deprecated ] ] , B [ [ deprecated ] ] = 42 } ;
(C++17以降)

非推奨として宣言されていない名前は、後で非推奨として再宣言することができます。非推奨として宣言された名前は、この属性なしで再宣言しても非推奨状態を解除することはできません。

#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ドキュメント for deprecated