C++C++14C++17C++20Learn C++

What Does Deprecated Mean And How To Use Deprecated Attribute In C++?

What Does Deprecated Mean And How To Use Deprecated Attribute In C++

C++ is very strong in every aspect of modern programming and evolves and refines itself even further with the release of each new C++ standard. While there are many new additions there are some features that are found not useful or dangerous in programming, thus the community offers its deprecation in the next standard and then it is generally removed in the following standard. C++14 came with a new deprecated attribute feature to allow marking an entity as deprecated – potentially obsolete and best avoided. In this post, we will explain what deprecated means and how can we use the [[deprecated]] and __declspec(deprecated) attributes in modern C++.

What does deprecated mean in C++?

Deprecation is the process of taking an older code part (a method, function, or property) and marking it as no longer being useful within the current version of the programming codebase (generally it is a standard, i.e. C++11, C++14) . It also flags that the deprecated feature will probably be removed in the next version of C++. Often this is because a feature or function has been found to be ‘dangerous’ or has been superseded or obsoleted by newer code. The deprecated attribute does not mean it is immediately removed from that C++ version (a standard or a library) because doing so may cause regression errors. Generally, a deprecated feature is going to be removed in the next version or standard. For example, the gets() function was found to have potentially dangerous (undesirable) behavior and it was deprecated in the C++11 standard and removed in the C++14 standard. Briefly, deprecated means, that part of code is considered outdated and not recommended to use, and that the keyword, feature, or function will probably be removed in a subsequent C++ standard.

How can we use the deprecated attribute in C++?

C++14, came with a new deprecated attribute feature to allow marking an entity deprecated, There are two options to declare something as deprecated. We can use [[deprecated]] and __declspec(deprecated) attributes.

The deprecated attribute is very useful to mark your older functions that probably will be removed. When you are working on a code base which you share with other developers it is useful for them to know that a section of code they are viewing is potentially outdated or will be removed – and the deprecated attribute is the best way to do that. While this is necessary for compiler developers (i.e. C++ Builder, GCC, Visual C++), it is also very desirable for library developers. If you find any code that will be removed in the next version, first, you can release a version that has the affected code part marked as deprecated. Then in the next release, you can remove it from your library. This is the modern way of professional programming.

The [[deprecated]] attribute allows marking the part of the code entity deprecated, which means it is still available to use but gives a warning from the compiler that the use of the code section is deprecated, it is discouraged and may allow ‘dangerous’ errors on runtime. Generally, a warning message is printed during compilation, and in the new modern IDEs there are warning icons displayed too as shown in the example below.

What Does Deprecated Mean And How To Use Deprecated Attribute In C++ Example of deprecated in the IDE

Is there a simple example of how to use the deprecated attribute in C++?

To deprecate a function, we can use [[deprecated]] attribute in front of a function.

Here is the syntax (Since C++14).

or we can use string literal that could be used to explain the rationale for deprecation, and we can suggest a replacing entity version etc.

these both syntaxes are still supported in the newer standards C++20 and C++23 too.

Here is a simple example that how we use [[deprecated]] attribute in C++.

Note that this attribute can be applied to the declaration of a function, a method of a class, a class, an enumeration, a typedef-name, a variable, a non-static data member, or a template specialization.

How can we use __declspec(deprecated) in C++?

C++ Builder and Visual C++, has also another option to declare deprecated, this is a specific deprecated declspec declaration, here is the syntax:

Or it can be used with a string literal as below.

and here is a simple example of how we can use it:

Is there a full example of deprecated attributes in modern C++?

Here is a full example to deprecated attribute in modern C++.

For more newer deprecated facilities and details you can check the “Reviewing Deprecated Facilities of C++20 for C++23” document – https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2020/p2139r2.html

What Does Deprecated Mean And How To Use Deprecated Attribute In C++ C++ Builder logo

C++ Builder is the easiest and fastest C and C++ IDE for building simple or professional applications on the Windows, MacOS, iOS & Android operating systems. It is also easy for beginners to learn with its wide range of samples, tutorials, help files, and LSP support for code. RAD Studio’s C++ Builder version comes with the award-winning VCL framework for high-performance native Windows apps and the powerful FireMonkey (FMX) framework for cross-platform UIs.

There is a free C++ Builder Community Edition for students, beginners, and startups; it can be downloaded from here. For professional developers, there are Professional, Architect, or Enterprise version.

Oh hi there 👋
It’s nice to meet you.

Sign up to receive awesome C++ content in your inbox, every day.

We don’t spam! Read our privacy policy for more info.

About author

Dr. Yilmaz Yoru has 35+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux, and some other operating systems. He graduated and received his MSc and PhD degrees from the Department of Mechanical Engineering of Eskisehir Osmangazi University. He is the founder and CEO of ESENJA LLC Company. His interests are Programming, Thermodynamics, Fluid Mechanics, Artificial Intelligence, 2D & 3D Designs, and high-end innovations.
Related posts
C++C++11C++14C++17C++20Learn C++Syntax

What is the negation (std::negation) metafunction in C++?

C++C++11C++14C++17C++20Learn C++Syntax

What Is Multiset (std::multiset) In Modern C++?

C++C++11C++14C++17C++20Learn C++Syntax

What Is Set (std::set) In Modern C++?

C++C++11C++14C++17C++20IteratorsLearn C++

What Is Deque (std::deque) In Modern C++?