工会中的标量成员是否计入共同的初始序列? [英] Do scalar members in a union count towards the common initial sequence?

查看:87
本文介绍了工会中的标量成员是否计入共同的初始序列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的联盟U 中,如果 a b 是活动成员,是否已定义访问 c 的行为?

In the union U below, if a or b is the active member, is it defined behavior to access c?

struct A{
    int a;
};
struct B{
    int a;
    double b;
};
union U{
    A a;
    B b;
    int c;
};






[class.union] ,该标准定义了一些使用的规则工会更容易(强调我的):


In [class.union], the standard defines some rules to make using a union easier (emphasis mine):


[注意:一项特殊保证是为了简化联合的使用而进行的操作:如果标准布局的联合包含多个共享公共初始序列的标准布局结构,并且此标准对象的非静态数据成员-layout联合类型是活动的,并且是标准布局结构之一,允许检查任何标准布局 struct 成员的公共初始序列;参见[class.mem]。 — —尾注]

[ Note: One special guarantee is made in order to simplify the use of unions: If a standard-layout union contains several standard-layout structs that share a common initial sequence, and if a non-static data member of an object of this standard-layout union type is active and is one of the standard-layout structs, it is permitted to inspect the common initial sequence of any of the standard-layout struct members; see [class.mem].  — end note ]

我在这里挂了 struct 一词。即使不是结构,像 int 这样的标准布局标量也算在内吗?

I'm hung up on the word struct here. Does a standard layout scalar like int count even though it's not a struct?


  • 我上面的 Union U 确实是 [class] 基本上表示它必须是使用 union 关键字的标准布局类,并且由于

  • My union U above is indeed a "standard-layout" union following [class] that basically says it needs to be a standard layout class that uses the union keyword instead, and since we only use scalars (standard layout types), it passes.

这些结构显然共享一个常见的初始序列,它由第一个 int 成员组成,但是尚不清楚是否可以将基本类型用于常见的初始序列。

The structs obviously share a common initial sequence that consists of the first int member, but it's unclear if fundamental types can be considered for common initial sequences.


  • [class.union] 还说,每个非静态数据成员都被分配为如果它是结构的唯一成员。

  • [class.union] also says that "Each non-static data member is allocated as if it were the sole member of a struct." which I think gives evidence that it is defined.

推荐答案

结构A struct B 是:


  • 包含在标准布局 union U

  • 标准布局结构和

  • 共享一个共同的初始序列。

因此,它们满足句子如果标准布局联合包含se共用一个初始序列的垂直标准版式结构…。

So, they satisfy the description in the sentence "If a standard-layout union contains several standard-layout structs that share a common initial sequence…".

int c 也位于联合不是这样的结构,也不是这样的结构。因此,这句话并没有告诉您可以写到 c 并检查 aa ba ,也不能写入 aa ba 并检查 c

The int c that is also in the union is not such a struct nor is it in such a struct. So this sentence is not telling you that you can write to c and inspect a.a or b.a, nor that you can write to a.a or b.a and inspect c.

这意味着 c 不是常见初始序列的一部分您可以检查。但这不会破坏 struct A struct B 的通用初始序列。

This means that c is not part of the common initial sequence you can inspect. But neither does it spoil the common initial sequence of struct A and struct B.

关于文本每个非静态数据成员被分配为好像它是结构的唯一成员,此处的标准有点草率。分配通常指获取或保留存储,但是这种用法似乎是指在给定的存储空间中布置对象的字节。在C ++标准中我没有看到正式的定义(但是我看起来并不难),但是我确实找到了类似的用法。

Regarding the text "Each non-static data member is allocated as if it were the sole member of a struct," the standard is being a bit sloppy with language here. Allocation usually refers to acquiring or reserving storage, but this use seems to refer to laying out the bytes of an object within given storage. I do not see a formal definition in the C++ standard (but I did not look too hard), but I did find a similar use. So I take it to mean that each non-static data member is laid out as if it were the sole member.

意思是指向这些联合中的任何一个的指针成员指向与其他任何工会成员的指针相同的位置,这可能意味着可以将指向一个工会成员的指针转换为指向另一个工会成员的指针,但是,它并不授予您违反严格别名规则的许可。如果 x c 和 y 是指向 a aa的指针,当 a时,不能使用 * x 访问 c 是最后写入的成员,或使用 * y ,而 c 是最后写入的成员成员。

What this says is that a pointer to any one of these union members points to the same place as a pointer to any of the other union members. This may imply that pointers to one can be converted to pointers to the other. However, it does not give you license to violate the strict-aliasing rules. Even if x is a pointer to c and y is a pointer to a or a.a, you cannot use *x to access c while a is the last-written member or use *y while c is the last-written member.

这篇关于工会中的标量成员是否计入共同的初始序列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆