📜  C ++ 14中不推荐使用的属性,带有示例

📅  最后修改于: 2021-05-31 23:00:29             🧑  作者: Mango

在本文中,我们将讨论C++ 14中的Deprecated属性。不推荐使用是指允许使用通过此属性声明的名称或实体,但由于某些原因不建议使用。编译器会给出警告,如果提供了字符串字面量,则它们将包含在警告中。

程序1:

例如,让我们考虑以下两个数字相乘的程序:

C++14
// C++14 program to multiply two
// numbers
#include 
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;
}


C++14
// C++14 program to illustrate the use
// of Deprecated attribute
#include 
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;
}


输出:
8

说明:在上面的程序中,借助于一个简单的名为乘法的函数执行了乘法运算。

现在,要告诉程序员这种方法已经过时,请使用以下语法通过不推荐使用该函数来执行另一种方法:

程式2:

C++ 14

// C++14 program to illustrate the use
// of Deprecated attribute
#include 
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;
}

输出:

可以弃用的清单如下:

  • 职能
  • 类,结构,联合
  • 多变的
  • 类型定义
  • 非静态数据成员
  • 命名空间
  • 枚举
  • 模板专业化
要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”