Open In App

Deprecated attribute in C++14 with Examples

Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will discuss Deprecated attribute in C++14. Deprecated means the use of the name or entity declared with this attribute is allowed but discouraged for some reason. The compiler gives warnings and if string literals are provided, they are included in warnings.

Program 1:

For example, let’s consider the following program of multiplication of two numbers:

C++14




// C++14 program to multiply two
// numbers
#include <iostream>
using namespace std;
  
// Function that returns the
// multiplication of two numbers
// a and b
int multiply(int a, int b)
{
    return a * b;
}
  
// Driver Code
int main()
{
    int a = 2, b = 4;
  
    // Function Call
    cout << multiply(a, b);
  
    return 0;
}


Output:

8

Explanation: In the above program, a multiplication operation has been performed with the help of a simple function named multiply.

Now, to tell the programmers that this approach is outdated, perform another approach by deprecating the function using the below syntax:

[[deprecated (“Write your message”)]]

Program 2:

C++14




// C++14 program to illustrate the use
// of Deprecated attribute
#include <iostream>
using namespace std;
  
// Deprecated message
[[deprecated("This method is outdated, use any other approach")]]
  
    // Now this function has been deprecated
    int
    multiply(int a, int b)
{
    return a * b;
}
  
// Driver Code
int main()
{
    int a = 2, b = 4;
  
    // Function Call
    cout << multiply(a, b);
  
    return 0;
}


Output:

Lists of what can be deprecated are as follows:



Last Updated : 28 Jan, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads