<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          C++核心準(zhǔn)則編譯邊學(xué)-F.22 使用T*或onwer指明唯一對(duì)象


          37653a329b0e5c3df8034eaf33bbbecf.webp

          F.22: Use T* or owner to designate a single object(使用T*或owner指明唯一對(duì)象)

          Reason(原因)

          Readability: it makes the meaning of a plain pointer clear. Enables significant tool support.

          可讀性:這可以讓裸指針的含義更明確。使重要的工具支持有效。


          譯者注

          譯者注:owner是gsl(準(zhǔn)則支持庫(kù))提供的一個(gè)功能,從編譯的角度來(lái)看和T*的含義一致,但是附加了所有權(quán)語(yǔ)義,可以幫助程序員理解代碼和工具檢查。

          Note(注意)

          In traditional C and C++ code, plain T* is used for many weakly-related purposes, such as:

          在傳統(tǒng)的C和C++代碼中,裸指針用于很多沒有什么關(guān)系的目的,例如:

          • Identify a (single) object (not to be deleted by this function)

            表示一個(gè)(單一)對(duì)象(不會(huì)被本函數(shù)刪除)

          • Point to an object allocated on the free store (and delete it later)

            指向一個(gè)從自由存儲(chǔ)上獲取的對(duì)象(以后會(huì)刪除)

          • Hold the nullptr

            持有nullptr

          • Identify a C-style string (zero-terminated array of characters)

            表示一個(gè)C風(fēng)格字符串(以0結(jié)尾的字符數(shù)組)

          • Identify an array with a length specified separately

            表示一個(gè)數(shù)組,長(zhǎng)度被另外定義

          • Identify a location in an array

            表示數(shù)組的首地址

          This makes it hard to understand what the code does and is supposed to do. It complicates checking and tool support.

          這樣做的結(jié)果是很難理解代碼在做什么和被預(yù)期做什么。無(wú)論是檢查還是工具支持都會(huì)復(fù)雜。

          Example(示例)
          void use(int* p, int n, char* s, int* q){    p[n - 1] = 666; // Bad: we don't know if p points to n elements;                    // assume it does not or use span    cout << s;      // Bad: we don't know if that s points to a zero-terminated array of char;                    // assume it does not or use zstring    delete q;       // Bad: we don't know if *q is allocated on the free store;                    // assume it does not or use owner}

          譯者注:這可能是我們每天都能見到的指針用法,很難看出它們到底是前面提供的那種用法。


          better(稍好)

          void use2(span<int> p, zstring s, owner<int*> q){    p[p.size() - 1] = 666; // OK, a range error can be caught    cout << s; // OK    delete q;  // OK}


          Note(注意)

          owner represents ownership, zstring represents a C-style string.

          owner表現(xiàn)所有權(quán),zstring表達(dá)C風(fēng)格的字符串


          Also: Assume that a T* obtained from a smart pointer to T (e.g., unique_ptr) points to a single element.

          參考:假定T*是從指向T的智能指針(例如unique_prt)獲取,指向單一對(duì)象的指針。


          See also: Support library

          參考:支持庫(kù)。

          See also: Do not pass an array as a single pointer

          參考:不要使用單一指針傳遞數(shù)組

          Enforcement(實(shí)施建議)
          • (Simple) ((Bounds)) Warn for any arithmetic operation on an expression of pointer type that results in a value of pointer type.

            (簡(jiǎn)單)((邊界))警告所有針對(duì)指針表達(dá)式,并得到指針類型結(jié)果的算數(shù)操作。


          瀏覽 109
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          評(píng)論
          圖片
          表情
          推薦
          C++核心準(zhǔn)則編譯邊學(xué)-F.8 優(yōu)先選擇純函數(shù)
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  操逼视频,国产操逼片 | 亚洲日本韩国一区二区在线 | 国精产品自偷自偷综合欧美 | 夜噜噜久久国产欧美日韩精品 | 不用播放器的AV网站 |