本篇內(nèi)容主要講解“C++怎么用not_null定義不能為空的指針”,感興趣的朋友不妨來(lái)看看。本文介紹的方法操作簡(jiǎn)單快捷,實(shí)用性強(qiáng)。下面就讓小編來(lái)帶大家學(xué)習(xí)“C++怎么用not_null定義不能為空的指針”吧!
專注于為中小企業(yè)提供網(wǎng)站設(shè)計(jì)、網(wǎng)站建設(shè)服務(wù),電腦端+手機(jī)端+微信端的三站合一,更高效的管理,為中小企業(yè)撫寧免費(fèi)做網(wǎng)站提供優(yōu)質(zhì)的服務(wù)。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動(dòng)了1000多家企業(yè)的穩(wěn)健成長(zhǎng),幫助中小企業(yè)通過(guò)網(wǎng)站建設(shè)實(shí)現(xiàn)規(guī)模擴(kuò)充和轉(zhuǎn)變。
not_null(
用not_null定義不能為空的指針)
To help avoid dereferencing nullptr
errors. To improve performance by avoiding redundant checks for nullptr
.
為了防止解引用nullptr錯(cuò)誤。為了避免多余的空指針檢查以提高性能。
譯者注:解引用就是指針前面加*獲取內(nèi)容的操作。
Example(示例)
int length(const char* p); // it is not clear whether length(nullptr) is valid
length(nullptr); // OK?
int length(not_null<const char*> p); // better: we can assume that p cannot be nullptr
int length(const char* p); // we must assume that p can be nullptr
By stating the intent in source, implementers and tools can provide better diagnostics, such as finding some classes of errors through static analysis, and perform optimizations, such as removing branches and null tests.
通過(guò)在代碼中說(shuō)明目的,實(shí)現(xiàn)者和工具可以提供較好的診斷,例如通過(guò)靜態(tài)分析發(fā)現(xiàn)某些類型的錯(cuò)誤,執(zhí)行性能優(yōu)化,例如移除分支和空判斷。
譯者注:指的是上述代碼中使用not_null的情況。
Note(注意)
not_null
is defined in the guidelines support library.
not_null在準(zhǔn)則支持庫(kù)中被定義。
譯者注:積極地使用準(zhǔn)則庫(kù)。
Note(注意)
The assumption that the pointer to char
pointed to a C-style string (a zero-terminated string of characters) was still implicit, and a potential source of confusion and errors. Use czstring
in preference to const char*
.
指向字符的指針會(huì)指向一個(gè)C風(fēng)格的字符串(以0結(jié)尾的字符串)這樣一個(gè)假設(shè)還是沒有說(shuō)清楚,這會(huì)成為不確定性和錯(cuò)誤的來(lái)源。在引用const char*時(shí)使用czstring。
// we can assume that p cannot be nullptr// we can assume that p points to a zero-terminated array of charactersint length(not_null<zstring> p);
Note: length()
is, of course, std::strlen()
in disguise.
注意:length()當(dāng)然是偽裝的std::strlen。
Enforcement(實(shí)施建議)
(Simple) ((Foundation)) If a function checks a pointer parameter against nullptr
before access, on all control-flow paths, then warn it should be declared not_null
.
(簡(jiǎn)單)((基礎(chǔ)))如果一個(gè)函數(shù)在使用指針參數(shù)之前在所有控制流路徑上進(jìn)行空檢查,那么發(fā)出應(yīng)該參數(shù)被聲明為not_null的警告信息。
(Complex) If a function with pointer return value ensures it is not nullptr
on all return paths, then warn the return type should be declared not_null
.
(復(fù)雜)如果一個(gè)指針類型(返回值)函數(shù)在它的所有返回路徑上都確認(rèn)返回值不為空,那么發(fā)出返回值應(yīng)該被定義為not_null的警告信息。
到此,相信大家對(duì)“C++怎么用not_null定義不能為空的指針”有了更深的了解,不妨來(lái)實(shí)際操作一番吧!這里是創(chuàng)新互聯(lián)網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!
當(dāng)前文章:C++怎么用not_null定義不能為空的指針
分享網(wǎng)址:http://chinadenli.net/article4/gissoe.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站設(shè)計(jì)公司、服務(wù)器托管、靜態(tài)網(wǎng)站、微信公眾號(hào)、全網(wǎng)營(yíng)銷推廣、小程序開發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)