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 되었음을 나타냅니다. 즉, 사용은 허용되지만 어떤 이유로 권장되지 않음을 의미합니다.

목차

구문

[ [ deprecated ] ] (1)
[ [ deprecated ( 문자열 리터럴 ) ] ] (2)
string-literal - 폐기 이유를 설명하고/또는 대체 개체를 제안하는 데 사용할 수 있는 unevaluated string literal

설명

이 속성으로 선언된 이름이나 개체의 사용이 허용되지만 어떤 이유로 권장되지 않음을 나타냅니다. 컴파일러는 일반적으로 이러한 사용에 대해 경고를 발생시킵니다. 지정된 string-literal 은 일반적으로 경고 메시지에 포함됩니다.

이 속성은 다음 이름 또는 엔티티의 선언에서 허용됩니다:

  • [ [ deprecated ] ] typedef S * PS ; ,
  • using PS [ [ deprecated ] ] = S * ; ,
  • (비멤버) 변수, 예를 들어, [ [ 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부터)

비사용 권고로 선언되지 않은 이름은 나중에 사용 권고로 재선언될 수 있습니다. 사용 권고로 선언된 이름은 이 속성 없이 재선언하여 사용 권고 상태를 해제할 수 없습니다.

예제

#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 Deprecated 속성 [dcl.attr.deprecated]
  • C++17 표준(ISO/IEC 14882:2017):
  • 10.6.4 Deprecated 속성 [dcl.attr.deprecated]
  • C++14 표준(ISO/IEC 14882:2014):
  • 7.6.5 Deprecated 속성 [dcl.attr.deprecated]

참고 항목

C 문서 참조 deprecated