Introduced by: C++14
deprecated is a new feature of C++14 standard. It can be mostly useful for programmers who develop classes shared between various teams or projects.
When a method of some class becomes obsolete and this class is used only in its owners’ team, it can be just removed and all the occurrences of this function call can be replaced with new valid method calls. This operation becomes unrealizable when the function is a part of shared interface used by various teams. Deleting the obsolete method would be harmful in that case; we cannot know who, where and how often is calling that method and how long they would need to refactor their code to suit new interface. We want to allow them using the deprecated function.
Anyway we would like to tell them somehow that they use the obsolete function and it’s recommended to replace all the calls. And that’s the purpose of attribute
deprecated in C++14.
Deprecated in short words
Calling the method marked as deprecated will result in compiler warning. It’s possible to provide custom warning message.
Example of usage
Calling the method
run() will result in compiler warning:
warning: ‘void CSharedInterface::run()’ is deprecated (declared at mainwindow.h:9) [-Wdeprecated-declarations]
Custom warning message can point to new recommended method.
[[deprecated("use start instead")]]
Then the compiler gives warning:
warning: ‘void CSharedInterface::run()’ is deprecated (declared at mainwindow.h:9): use start instead [-Wdeprecated-declarations]
Not only methods can be
deprecated; it’s also possible to mark classes, variables, typedefs, enums and other entities.