前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >C++核心准则C.182:使用匿名联合体实现附带标签的联合体

C++核心准则C.182:使用匿名联合体实现附带标签的联合体

作者头像
面向对象思考
发布2020-03-25 17:01:26
7360
发布2020-03-25 17:01:26
举报
文章被收录于专栏:C++核心准则原文翻译

C.182: Use anonymous unions to implement tagged unions

C.182:使用匿名联合体实现附带标签的联合体

Reason(原因)

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

良好设计的命名联合体是类型安全的。无名联合体简化了包含(标签,联合体)对的类的设计。

Example(示例)

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

这段示例代码主要借用自TC++PL4的216页到218页(中文版:C++程序设计语言(原书第四版)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.

这段代码有些复杂。使用用户定义的赋值和析构函数处理一种类型不是那么容易。把程序员从必须编写这样的代码的情况中解救出来是在标准库中增加variant的原因之一。

代码语言:javascript
复制
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
}
使用匿名联合体的好处就是在使用其成员时,不需要A.B的形式。

--译者注

Enforcement(实施建议)

???

原文链接:

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


觉得本文有帮助?请分享给更多人。

关注【面向对象思考】轻松学习每一天!

面向对象开发,面向对象思考!

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2020-03-09,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 面向对象思考 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • C.182: Use anonymous unions to implement tagged unions
  • Reason(原因)
    • 使用匿名联合体的好处就是在使用其成员时,不需要A.B的形式。
      • Enforcement(实施建议)
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档