網(wǎng)站首頁 編程語言 正文
前言
在C++ 17中引入了一個標記符nodiscard,用于聲明一個 “非棄值(no-discard)表達式”。那么在開始之前,我們需要了解一下什么是棄值表達式。
棄值表達式
棄值表達式,就是放棄獲取返回值的表達式。首先棄值表達式的返回值是非void類型的。一般,我們使用的棄值表達式,其返回值只是起次要的作用,而其本身的作用占主要。比如++i;就是一個棄值表達式,它的主要作用就是累加,但同時我們也可以選擇獲取其累加的返回值,只不過這是次要的。
再比如,C標準庫的文件寫入函數(shù),其聲明如下:
int __cdecl fputs(const char * __restrict__ _Str,FILE * __restrict__ _File);
它有一個int類型的返回值,用于獲取寫入狀態(tài),它的主要作用是寫入文件,我可以選擇不獲取狀態(tài),也可以選擇獲取狀態(tài):
fputs("Hello World",pFile);
int result = fputs("Hello World",pFile);
nodiscard標記符
那么我如果想向用戶建議獲取返回值,這時候,我就可以使用nodiscard標記符。它一般用于標記函數(shù)的返回值或者某個類。聲明語法為:
/* @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>將其轉(zhuǎn)化為void時,編譯器會拋出warning來提醒用戶獲取返回值。
函數(shù)非棄值聲明
[[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
}
結(jié)果如下:
類/枚舉類/結(jié)構(gòu) 非棄值聲明
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{};
? ? ? | ? ? ? ? ? ? ? ? ? ? ?^
返回類引用與類指針
當返回值為引用或者指針的 類/枚舉類/結(jié)構(gòu)(函數(shù)不行) 時,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
- 上一篇:沒有了
- 下一篇:沒有了
相關(guān)推薦
- 2022-06-18 Android?Recyclerview實現(xiàn)左滑刪除功能_Android
- 2022-06-25 Python利用format函數(shù)實現(xiàn)對齊打印(左對齊、右對齊與居中對齊)_python
- 2022-06-07 Python接口自動化之文件上傳/下載接口詳解_python
- 2022-04-25 一起來看看C語言世界中的結(jié)構(gòu)體_C 語言
- 2022-08-23 多線程python的實現(xiàn)及多線程有序性_python
- 2022-04-25 ASP.NET?Core中Razor頁面與MVC區(qū)別介紹_實用技巧
- 2022-07-23 SQL?Server中T-SQL標識符介紹與無排序生成序號的方法_MsSql
- 2022-04-01 報錯處理-bash: fork: Cannot allocate memory
- 欄目分類
-
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支