In this C++ Tutorial we will discuss the Deprecated attribute introduced in C++14. Sometimes while using C++ libraries, (or any programming library for that matter) we may see a warning message in the console saying “This function is deprecated and will be removed in a later version”.
What does this mean? Well, deprecation refers to when an entity (such as a function or class) has been considered “outdated” and should no longer be used. There can be many reasons behind this, such as an entity being replaced by a newer version, or a better alternative was introduced. Or maybe due to security concerns.
In this tutorial we will explore how to add this Deprecation attribute to our own entities.
If you aren’t sure what version of C++ you have, take a quick look at this tutorial right here on how to check your C++ compiler version.
How to marks entities as Deprecated in C++
Marking an entity as deprecated is extremely simple. All you need to do is insert this statement in the appropriate location, [[deprecated]]
. The method for marking a function as deprecated is shown below.
[[deprecated]]
void func() {
return;
}
int main() {
func(); // deprecated function
}
Now if you compile and run your code, the following warning will appear in the console.
warning: 'void func()' is deprecated
You can also choose to add a “Deprecation message” as well, which provides some useful information to the person trying to use this function. This message typically explains why the function was deprecated, and tells you which function you should be using instead (a better alternative or an updated version).
#include <iostream>
using namespace std;
[[deprecated("Deprecated due to xxxx. Use xxxx instead")]]
void func() {
return;
}
int main() {
func(); // deprecated function
}
warning: 'void func()' is deprecated: Deprecated due to xxxx. Use xxxx instead
It is important to remember that marking an entity as deprecated does not throw an error. It just throws a warning. Your program will execute normally. A deprecation warning mainly serves as an indicator
Deprecation warnings are not limited to just functions. We can apply them in several different places, such as Class, Enums and Variables. Let’s take a look at some more examples.
Mark Variable as Deprecated
[[deprecated]]
int var = 0;
int main() {
std::cout << var << std::endl;
}
Mark Class as Deprecated in C++
The method for applying a deprecation warning on Classes is the same for structures (struct) as well.
class [[deprecated]] MyClass {
};
int main() {
MyClass myclass = MyClass();
}
Mark Class Method as Deprecated
Sometimes just a single method in the Class will be deprecated, not the whole Class.
class MyClass {
public:
[[deprecated]] void func() {}
};
int main() {
MyClass myclass = MyClass();
myclass.func();
}
Mark Function Parameter as Deprecated
class MyClass {
public:
void func () {}
void func([[deprecated]] int x) {
cout << x << endl;
}
};
int main() {
MyClass myclass = MyClass();
myclass.func(); // not deprecated
myclass.func(6); // deprecated
}
Mark Enum as Deprecated
enum [[deprecated]] week {
Sunday,
Monday,
Tuesday,
Wednesday,
Thursday,
Friday,
Saturday
};
int main() {
week e;
}
This marks the end of the C++ deprecated attribute (C++14) Tutorial. Any suggestions or contributions for CodersLegacy are more than welcome. Questions regarding the tutorial content can be asked in the comments section below.