deprecated attribute

Introduced by: C++14

Attribute 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

class CSharedInterface
{
public:
    [[deprecated]]
    void run();
};

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")]]
    void run();

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.

Advertisements

3 thoughts on “deprecated attribute

  1. Cleroth Sun says:

    This may have a hidden temporary usage: find all places where a function is being called when find references or refactoring tools sometimes don’t catch everything, and actually removing the declaration potentially causes a very long list of errors.

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s