C++14新特性:deprecated的含義與使用?

一、小序

學習java或者python的同學可能都知道deprecated這個詞,搞C++的同學可能對這個詞還比較陌生,畢竟這個詞直到C++14纔出現,並且使用的頻率可能也不是很高。下面一塊來看一下deprecated這個詞的神祕面紗。

二、含義

直譯這個詞的意思是“不贊成、不建議、強烈反對”,在java或者python中可以在這個詞前面加上@即@deprecated表示註解,C++中的功能也類似,用來表示一個名字或者實體不再推薦使用或者已經棄用,通常已經有了更好的方法來代替這個名字或者實體,只不過爲了兼容之前的老代碼可能還要保留原來這個名字或者實體,但目前C++官方文檔並不鼓勵使用此屬性。

三、使用方法

1、語法

在C++中deprecated有兩種語法格式,一種不需要加字符字面值,一種需要加字符字面值,形式如下:
[[deprecated]]
[[deprecated(字符字面值)]]

2、使用

deprecated在C++中的使用範圍很廣泛,大部分時候我們可能只在函數前使用,以下是摘自官方的使用範圍,我們最常用的可能就是在函數上使用。

1》class/struct/union: struct [[deprecated]] S;,
2》typedef-name, including those declared by alias declaration: [[deprecated]] typedef S* PS;,
using PS [[deprecated]] = S*;,
3》variable, including static data member: [[deprecated]] int x;,
4》non-static data member: union U { [[deprecated]] int n; };,
5》function: [[deprecated]] void f();,
6》namespace: namespace [[deprecated]] NS { int x; }
7》enumeration: enum [[deprecated]] E {};,
8》enumerator: enum { A [[deprecated]], B [[deprecated]] = 42 };.
9》template specialization: template<> struct [[deprecated]] X {};

原創不易,點贊鼓勵一下吧!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章