<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)則T.47:避免使用通用名稱的高度不受限模板

          T.47: Avoid highly unconstrained templates with common names

          T.47:避免使用通用名稱的高度不受限模板


          Reason(原因)

          An unconstrained template argument is a perfect match for anything so such a template can be preferred over more specific types that require minor conversions. This is particularly annoying/dangerous when ADL is used. Common names make this problem more likely.

          不受限的模板參數(shù)會完美匹配任何東西,因此這樣的模板可以覆蓋需要輕微轉(zhuǎn)換的特定類型。當(dāng)使用ADL時,這種情況很麻煩/危險。通用的名稱會讓這個問題更容易發(fā)生。


          Example(示例)

          namespace Bad {
          struct S { int m; };
          template
          bool operator==(T1, T2) { cout << "Bad\n"; return true; }
          }

          namespace T0 {
          bool operator==(int, Bad::S) { cout << "T0\n"; return true; } // compare to int

          void test()
          {
          Bad::S bad{ 1 };
          vector v(10);
          bool b = 1 == bad;
          bool b2 = v.size() == bad;
          }
          }

          This prints?T0?and?Bad.

          代碼會打印T0和Bad。

          Now the?==?in?Bad?was designed to cause trouble, but would you have spotted the problem in real code? The problem is that?v.size()?returns an?unsigned?integer so that a conversion is needed to call the local?==; the?==?in?Bad?requires no conversions. Realistic types, such as the standard-library iterators can be made to exhibit similar anti-social tendencies.

          現(xiàn)在Bad中的==被設(shè)計用于引發(fā)問題,但是你能定位到實際代碼中的問題么?問題是v.size()返回一個無符號整數(shù),因此調(diào)用本地==時需要轉(zhuǎn)換;Bad中的==則不需要轉(zhuǎn)換。實際的類型,例如標(biāo)準(zhǔn)庫中的迭代器等有可能會表現(xiàn)出這種類似反社會問題的傾向。


          Note(注意)

          If an unconstrained template is defined in the same namespace as a type, that unconstrained template can be found by ADL (as happened in the example). That is, it is highly visible.

          如果不受限模板被定義在類型相同的命名空間,這個不受限模板可以被ADL發(fā)現(xiàn)(就像示例代碼中發(fā)生的那樣。)。也就是說,它是高度可見的。


          Note(注意)

          This rule should not be necessary, but the committee cannot agree to exclude unconstrained templated from ADL.

          本規(guī)則應(yīng)該是沒有必要的,但是委員會不能同意將非受限模板從ADL中排除出去。

          Unfortunately this will get many false positives; the standard library violates this widely, by putting many unconstrained templates and types into the single namespace?std.

          不幸的是,這會引發(fā)很多假陽性;標(biāo)準(zhǔn)庫將很多非受限模板放入std命名空間,這導(dǎo)致大量違反本規(guī)則的情況。


          Enforcement(實施建議)

          Flag templates defined in a namespace where concrete types are also defined (maybe not feasible until we have concepts).

          標(biāo)記定義在和具體類型定義在同一命名空間的模板(也許只有通過concept才可能實現(xiàn))


          原文鏈接

          https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#t47-avoid-highly-visible-unconstrained-templates-with-common-names


          新書介紹

          《實戰(zhàn)Python設(shè)計模式》是作者最近出版的新書,拜托多多關(guān)注!

          本書利用Python 的標(biāo)準(zhǔn)GUI 工具包tkinter,通過可執(zhí)行的示例對23 個設(shè)計模式逐個進(jìn)行說明。這樣一方面可以使讀者了解真實的軟件開發(fā)工作中每個設(shè)計模式的運用場景和想要解決的問題;另一方面通過對這些問題的解決過程進(jìn)行說明,讓讀者明白在編寫代碼時如何判斷使用設(shè)計模式的利弊,并合理運用設(shè)計模式。

          對設(shè)計模式感興趣而且希望隨學(xué)隨用的讀者通過本書可以快速跨越從理解到運用的門檻;希望學(xué)習(xí)Python GUI 編程的讀者可以將本書中的示例作為設(shè)計和開發(fā)的參考;使用Python 語言進(jìn)行圖像分析、數(shù)據(jù)處理工作的讀者可以直接以本書中的示例為基礎(chǔ),迅速構(gòu)建自己的系統(tǒng)架構(gòu)。




          覺得本文有幫助?請分享給更多人。

          關(guān)注微信公眾號【面向?qū)ο笏伎肌枯p松學(xué)習(xí)每一天!

          面向?qū)ο箝_發(fā),面向?qū)ο笏伎迹?/span>



          瀏覽 61
          點贊
          評論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報
          評論
          圖片
          表情
          推薦
          <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>
                  欧美亚洲福利 | 无吗中文 | 成人在线看黄片 | 大奶模特惜萍 | 请立即播放黑人大屌日白人小嫩逼的视频 |