<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)則C.182:使用匿名聯(lián)合體實(shí)現(xiàn)附帶標(biāo)簽的聯(lián)合體

          872a27f57b2b1af535fdb8cd1f223dd1.webp

          C.182: Use anonymous?unions to implement tagged unions

          C.182:使用匿名聯(lián)合體實(shí)現(xiàn)附帶標(biāo)簽的聯(lián)合體


          Reason(原因)

          A well-designed tagged union is type safe. An?anonymous?union simplifies the definition of a class with a (tag, union) pair.

          良好設(shè)計(jì)的命名聯(lián)合體是類型安全的。無名聯(lián)合體簡(jiǎn)化了包含(標(biāo)簽,聯(lián)合體)對(duì)的類的設(shè)計(jì)。


          Example(示例)

          This example is mostly borrowed from TC++PL4 pp216-218. You can look there for an explanation.

          這段示例代碼主要借用自TC++PL4的216頁(yè)到218頁(yè)(中文版:C++程序設(shè)計(jì)語(yǔ)言(原書第四版)p186-p188)。你可以查看該書中的解釋。


          The code is somewhat elaborate. Handling a type with user-defined assignment and destructor is tricky. Saving programmers from having to write such code is one reason for including?variant?in the standard.

          這段代碼有些復(fù)雜。使用用戶定義的賦值和析構(gòu)函數(shù)處理一種類型不是那么容易。把程序員從必須編寫這樣的代碼的情況中解救出來是在標(biāo)準(zhǔn)庫(kù)中增加variant的原因之一。

          class Value { // two alternative representations represented as a union
          private:
          enum class Tag { number, text };
          Tag type; // discriminant

          union { // representation (note: anonymous union)
          int i;
          string s; // string has default constructor, copy operations, and destructor
          };
          public:
          struct Bad_entry { }; // used for exceptions

          ~Value();
          Value& operator=(const Value&); // necessary because of the string variant
          Value(const Value&);
          // ...
          int number() const;
          string text() const;

          void set_number(int n);
          void set_text(const string&);
          // ...
          };

          int Value::number() const
          {
          if (type != Tag::number) throw Bad_entry{};
          return i;
          }

          string Value::text() const
          {
          if (type != Tag::text) throw Bad_entry{};
          return s;
          }

          void Value::set_number(int n)
          {
          if (type == Tag::text) {
          s.~string(); // explicitly destroy string
          type = Tag::number;
          }
          i = n;
          }

          void Value::set_text(const string& ss)
          {
          if (type == Tag::text)
          s = ss;
          else {
          new(&s) string{ss}; // placement new: explicitly construct string
          type = Tag::text;
          }
          }

          Value& Value::operator=(const Value& e) // necessary because of the string variant
          {
          if (type == Tag::text && e.type == Tag::text) {
          s = e.s; // usual string assignment
          return *this;
          }

          if (type == Tag::text) s.~string(); // explicit destroy

          switch (e.type) {
          case Tag::number:
          i = e.i;
          break;
          case Tag::text:
          new(&s) string(e.s); // placement new: explicit construct
          }

          type = e.type;
          return *this;
          }

          Value::~Value()
          {
          if (type == Tag::text) s.~string(); // explicit destroy
          }
          使用匿名聯(lián)合體的好處就是在使用其成員時(shí),不需要A.B的形式。

          --譯者注


          Enforcement(實(shí)施建議)

          ???


          原文鏈接:

          https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#c181-avoid-naked-unions




          覺得本文有幫助?請(qǐng)分享給更多人。

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

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


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

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          評(píng)論
          圖片
          表情
          推薦
          <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>
                  99精品大香蕉 | 欧美亚洲黄色片 | 日韩黄色一级 | 欧美操妣免费看 | 国产三级在线播放一 |