網站首頁 編程語言 正文
前言
在C++ 17中引入了一個標記符nodiscard,用于聲明一個 “非棄值(no-discard)表達式”。那么在開始之前,我們需要了解一下什么是棄值表達式。
棄值表達式
棄值表達式,就是放棄獲取返回值的表達式。首先棄值表達式的返回值是非void類型的。一般,我們使用的棄值表達式,其返回值只是起次要的作用,而其本身的作用占主要。比如++i;就是一個棄值表達式,它的主要作用就是累加,但同時我們也可以選擇獲取其累加的返回值,只不過這是次要的。
再比如,C標準庫的文件寫入函數,其聲明如下:
int __cdecl fputs(const char * __restrict__ _Str,FILE * __restrict__ _File);
它有一個int類型的返回值,用于獲取寫入狀態,它的主要作用是寫入文件,我可以選擇不獲取狀態,也可以選擇獲取狀態:
fputs("Hello World",pFile);
int result = fputs("Hello World",pFile);
nodiscard標記符
那么我如果想向用戶建議獲取返回值,這時候,我就可以使用nodiscard標記符。它一般用于標記函數的返回值或者某個類。聲明語法為:
/* @since C++17 */
[[nodiscard]] return_type function();
/* @since C++20 */
[[nodiscard("message")]] return_type function();
/* Standard lib defination */
/*
#if __cplusplus >= 201703L
# define _GLIBCXX_NODISCARD [[__nodiscard__]]
#else
# define _GLIBCXX_NODISCARD
#endif
*/
_GLIBCXX_NODISCARD return_type function();
如果一個被nodiscard標記了的表達式,如果我們在使用時棄值了,而且沒有使用static_cast<void>將其轉化為void時,編譯器會拋出warning來提醒用戶獲取返回值。
函數非棄值聲明
[[nodiscard]] int func1(){
return 1;
}
[[nodiscard("nodiscared function")]] int func2(){
return 1;
}
int main(){
func1(); //warning C++17
func2(); //warning c++20
int a = func1(); //no warning
static_cast<void>(func1()); //no warning
}
結果如下:
類/枚舉類/結構 非棄值聲明
class [[nodiscard]] A{};
enum class [[nodiscard]] B{X,Y};
struct [[nodiscard]] C{};
A createA(){
return A();
}
B createB(){
return B::X;
}
C createC(){
return C();
}
int main(){
createA();
createB();
createC();
}
輸出如下:
6.cpp: In function 'int main()':
6.cpp:22:12: warning: ignoring returned value of type 'A', declared with attribute 'nodiscard' [-Wunused-result]
? ?22 | ? ? createA();
? ? ? | ? ? ~~~~~~~^~
6.cpp:10:3: note: in call to 'A createA()', declared here
? ?10 | A createA(){
? ? ? | ? ^~~~~~~
6.cpp:6:21: note: 'A' declared here
? ? 6 | class [[nodiscard]] A{};
? ? ? | ? ? ? ? ? ? ? ? ? ? ^
6.cpp:23:12: warning: ignoring returned value of type 'B', declared with attribute 'nodiscard' [-Wunused-result]
? ?23 | ? ? createB();
? ? ? | ? ? ~~~~~~~^~
6.cpp:14:3: note: in call to 'B createB()', declared here
? ?14 | B createB(){
? ? ? | ? ^~~~~~~
6.cpp:7:26: note: 'B' declared here
? ? 7 | enum class [[nodiscard]] B{X,Y};
? ? ? | ? ? ? ? ? ? ? ? ? ? ? ? ?^
6.cpp:24:12: warning: ignoring returned value of type 'C', declared with attribute 'nodiscard' [-Wunused-result]
? ?24 | ? ? createC();
? ? ? | ? ? ~~~~~~~^~
6.cpp:18:3: note: in call to 'C createC()', declared here
? ?18 | C createC(){
? ? ? | ? ^~~~~~~
6.cpp:8:22: note: 'C' declared here
? ? 8 | struct [[nodiscard]] C{};
? ? ? | ? ? ? ? ? ? ? ? ? ? ?^
返回類引用與類指針
當返回值為引用或者指針的 類/枚舉類/結構(函數不行) 時,nodiscard 就無效了:
class [[nodiscard]] A{};
A& createAref(){
A* a = new A();
return *a;
}
A* createAptr(){
A* a = new A();
return a;
}
int main(){
createAref(); //no warning
createAptr(); //no warning
}
原文鏈接:https://blog.csdn.net/weixin_43130747/article/details/129222130
- 上一篇:沒有了
- 下一篇:沒有了
相關推薦
- 2022-08-04 Python并發編程之IO模型_python
- 2023-03-27 React?Router6.x路由表封裝的兩種寫法_React
- 2022-10-22 Android協程作用域與序列發生器限制介紹梳理_Android
- 2023-01-29 使用Python統計代碼運行時間的兩種方法_python
- 2023-11-15 Linux系統SSH客戶端斷開后保持進程繼續運行配置方法;Python等腳本在終端后臺運行的方法
- 2022-12-14 Python開發之利用re模塊去除代碼塊注釋_python
- 2022-07-14 C#把dll分別放在指定的文件夾的方法步驟_C#教程
- 2022-10-08 ASP.NET泛型三之使用協變和逆變實現類型轉換_實用技巧
- 欄目分類
-
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支