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++.
Table of Contents
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.
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).
1 2 3 |
[[deprecated]] <definition> |
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.
1 2 3 |
[[deprecated( string_literal )]] <definition> |
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++.
1 2 3 4 5 6 |
[[deprecated]] void myf() { } |
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:
1 2 3 |
__declspec(deprecated) <definition> |
Or it can be used with a string literal as below.
1 2 3 |
__declspec(deprecated( string-literal )) <definition> |
and here is a simple example of how we can use it:
1 2 3 4 5 |
__declspec(deprecated) void myf() { } |
Is there a full example of deprecated attributes in modern C++?
Here is a full example to deprecated attribute in modern C++.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
#include <iostream> [[deprecated]] void myf_ver1() { std::cout << "My function version 1\n"; } [[deprecated("Use myf_ver3()")]] void myf_ver2() { std::cout << "My function version 2\n"; } __declspec(deprecated) void myf_ver3() { std::cout << "My function version 3\n"; } __declspec(deprecated) void myf_ver4() { } int main() { myf_ver1(); myf_ver2(); myf_ver3(); myf_ver4(); system("pause"); return 0; } |
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
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.