📜 ⬆️ ⬇️

Attribute deprecated in C ++ 14

The normal development of any code is the obsolescence of its individual parts (functions, classes, etc.) and their systematic removal from the project to reduce the complexity and increase the security of the code. Simply removing something is usually a bad idea — it can dramatically break some component that uses the entity being deleted. A good practice is to mark the outdated code in some way that will allow programmers using it to know that it is scheduled for deletion. ( Approx. Translator - Microsoft invented its bicycle for this, and even people sometimes used the #pragma message ).

The new standard C ++ 14 introduces the deprecated attribute to mark obsolete entities that are planned to be removed in the future. The compiler can display warnings whenever you try to use them. The syntax is fundamentally new for C ++: attributes are supposed to be written as a list, separated by commas, inside double square brackets. Here is the function marked as deprecated:

[[deprecated]] void foo() {} int main() { foo(); //       } 


Other possible attributes: noreturn, carries_dependency, alignas
')
The code above, being compiled by the latest build of clang, gives the following message at the compilation stage:

 warning: 'foo' is deprecated [-Wdeprecated-declarations] foo(); ^ note: 'foo' has been explicitly marked deprecated here void foo() {} ^ 


It is also possible to display your own message:

 [[deprecated("use bar instead")]] void foo() {} This gives the following warning instead: warning: 'foo' is deprecated: use bar instead [-Wdeprecated-declarations] foo(); ^ note: 'foo' has been explicitly marked deprecated here void foo() {} ^ 


The deprecated attribute also applies to classes, typedefs, variables, non-static data members, enumerations, and templates. Here are a couple of examples:

 //   [[deprecated]] void foo(); //   [[deprecated]] int x; //        int y [[deprecated]], z; //    int triple([[deprecated]] int x); //     class [[deprecated]] my_class { public: //    [[deprecated]] int member; }; //   enum [[deprecated]] animals { CAT, DOG, MOUSE }; //  typedef [[deprecated]] typedef int type; //    template <typename T> class templ; template <> class [[deprecated]] templ<int> {}; 

For those who notice the striking lack of consistency in the placement of attributes (for example, their use after the class and enum keywords), I would say that this is so that you can distinguish the mark of the type itself from the mark of a variable of this type. For example, the following declaration marks a variable, but not the class itself:

 [[deprecated]] class C { } c; 


The deprecated attribute can be specified only in one entity definition; all the following declarations without this attribute do not remove the deprecated attribute. It is best to place the attribute in the header file - this will tell all users that the entity is outdated and will be deleted.

The attribute is supported starting with Clang 3.4 and GCC 4.9.

Translator's notes : the attributes that appeared in the C ++ 11 standard gave great promise - a new opportunity to add metadata to functions, classes and other things potentially opened up great opportunities for meta-programming, reflection, testing - in the end C # and Python already proved I really wanted to believe in the real use of such tools of the language, and I personally wanted to believe that C ++ would receive something similar in the future. Unfortunately, the first attributes entered ( noreturn , carries_dependency , alignas ) were somehow of little use, which spoiled the positive impression of the feature. deprecated - the first, in my opinion, really simple, understandable and necessary for everyone attribute. I want to believe that this is a step towards further features of the language based on attributes.

Source: https://habr.com/ru/post/231227/


All Articles