前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >C++核心准则C.181:避免使用"暴露的"联合体

C++核心准则C.181:避免使用"暴露的"联合体

作者头像
面向对象思考
发布2020-03-25 17:01:05
4150
发布2020-03-25 17:01:05
举报

C.181: Avoid "naked" unions

C.181:避免使用"暴露的"联合体

Reason(原因)

A naked union is a union without an associated indicator which member (if any) it holds, so that the programmer has to keep track. Naked unions are a source of type errors.

暴露的联合体指的是不包含用来表示哪个(如果存在的话)成员有效的标志的联合体,程序员必须对数据流保持跟踪。暴露状态的联合体是错误的源头之一。

Example, bad(反面示例)

代码语言:javascript
复制
union Value {
    int x;
    double d;
};

Value v;
v.d = 987.654;  // v holds a double

So far, so good, but we can easily misuse the union:

到目前为止还好,但是我们会很容易地错误使用这个联合体:

代码语言:javascript
复制
cout << v.x << '\n';    // BAD, undefined behavior: v holds a double, but we read it as an int

Note that the type error happened without any explicit cast. When we tested that program the last value printed was 1683627180 which is the integer value for the bit pattern for 987.654. What we have here is an "invisible" type error that happens to give a result that could easily look innocent.

注意类型错误是在没有任何显式类型转换的情况下发生的。但我们测试这段程序的时候,输出的最后一个值是1863627180,它是987.654的二进制对应的整数值。我们在这里遇到的是一个"不可见"类型错误,它恰巧给出一个很容易被判断为没有问题的结果。

And, talking about "invisible", this code produced no output:

另外,谈到"不可见",下面的代码不会产生输出:

代码语言:javascript
复制
v.x = 123;
cout << v.d << '\n';    // BAD: undefined behavior
Alternative(可选项)

Wrap a union in a class together with a type field.

将联合体和一个类型字段封装为一个类。

The C++17 variant type (found in <variant>) does that for you:

C++17的variant类型(可以在<variant>中找到)可以为你做同样的事:

代码语言:javascript
复制
variant<int, double> v;
v = 123;        // v holds an int
int x = get<int>(v);
v = 123.456;    // v holds a double
w = get<double>(v);
Enforcement(实施建议)

???

原文链接:

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


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

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

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

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

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

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

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

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • C.181: Avoid "naked" unions
  • Reason(原因)
    • Alternative(可选项)
      • Enforcement(实施建议)
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档