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 - невычисляемый строковый литерал, который может использоваться для объяснения причин устаревания и/или предложения заменяющей сущности

Объяснение

Указывает, что использование имени или сущности, объявленной с этим атрибутом, разрешено, но не рекомендуется по какой-либо причине. Компиляторы обычно выдают предупреждения при таких использованиях. string-literal , если указан, обычно включается в предупреждения.

Этот атрибут разрешен в объявлениях следующих имен или сущностей:

  • [ [ deprecated ] ] typedef S * PS ; ,
  • using PS [ [ deprecated ] ] = S * ; ,
  • перечислитель, например, enum { A [ [ deprecated ] ] , B [ [ deprecated ] ] = 42 } ; ,
(начиная с C++17)

Имя, объявленное без атрибута deprecated, может быть переобъявлено с атрибутом deprecated. Имя, объявленное с атрибутом deprecated, не может быть снято с устаревания путем переобъявления без этого атрибута.

Пример

#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 Устаревший атрибут [dcl.attr.deprecated]
  • Стандарт C++14 (ISO/IEC 14882:2014):
  • 7.6.5 Устаревший атрибут [dcl.attr.deprecated]

Смотрите также