c++ - 访问不活动的 union 成员和未定义的行为?

标签 c++ undefined-behavior language-lawyer unions

我的印象是访问除最后一组之外的 union 成员是 UB,但我似乎找不到可靠的引用(除了声称它是 UB 但没有任何来自标准的支持)。

那么,这是未定义的行为吗?

最佳答案

令人困惑的是,C 明确允许通过 union 进行类型双关,而 C++ ( ) 没有这样的权限。

6.5.2.3 Structure and union members

95) If the member used to read the contents of a union object is not the same as the member last used to store a value in the object, the appropriate part of the object representation of the value is reinterpreted as an object representation in the new type as described in 6.2.6 (a process sometimes called ‘‘type punning’’). This might be a trap representation.

C++的情况:

9.5 Unions [class.union]

In a union, at most one of the non-static data members can be active at any time, that is, the value of at most one of the non-static data members can be stored in a union at any time.

后来的 C++ 语言允许使用包含 struct 和公共(public)初始序列的 union ;但是,这不允许类型双关。

要确定在 C++ 中是否允许 union 类型双关 ,我们必须进一步搜索。回想一下 是 C++11 的规范性引用(并且 C99 与 C11 具有相似的语言,允许 union 类型双关):

3.9 Types [basic.types]

4 - The object representation of an object of type T is the sequence of N unsigned char objects taken up by the object of type T, where N equals sizeof(T). The value representation of an object is the set of bits that hold the value of type T. For trivially copyable types, the value representation is a set of bits in the object representation that determines a value, which is one discrete element of an implementation-defined set of values. 42
42) The intent is that the memory model of C++ is compatible with that of ISO/IEC 9899 Programming Language C.

当我们阅读时会变得特别有趣

3.8 Object lifetime [basic.life]

The lifetime of an object of type T begins when: — storage with the proper alignment and size for type T is obtained, and — if the object has non-trivial initialization, its initialization is complete.

因此,对于包含在 union 中的原始类型(ipso facto 具有微不足道的初始化),对象的生命周期至少包含 union 本身的生命周期。这允许我们调用

3.9.2 Compound types [basic.compound]

If an object of type T is located at an address A, a pointer of type cv T* whose value is the address A is said to point to that object, regardless of how the value was obtained.

假设我们感兴趣的操作是类型双关语,即获取非事件 union 成员的值,并且根据上述给出我们对该成员引用的对象的有效引用,那么该操作是左值到右值的转换:

4.1 Lvalue-to-rvalue conversion [conv.lval]

A glvalue of a non-function, non-array type T can be converted to a prvalue. If T is an incomplete type, a program that necessitates this conversion is ill-formed. If the object to which the glvalue refers is not an object of type T and is not an object of a type derived from T, or if the object is uninitialized, a program that necessitates this conversion has undefined behavior.

接下来的问题是,作为非事件 union 成员的对象是否通过存储初始化到事件 union 成员。据我所知,情况并非如此,尽管如果:

  • 将 union 复制到 char 数组存储并返回 (3.9:2),或者
  • 将一个 union 按字节复制到另一个相同类型的 union (3.9:3),或者
  • 通过符合 ISO/IEC 9899(就其定义而言)(3.9:4 注 42)的程序元素跨语言边界访问 union ,然后

非事件成员对 union 的访问已定义并被定义为遵循对象和值表示,没有上述插入之一的访问是未定义的行为。这对允许在此类程序上执行的优化有影响,因为实现当然可能假设未发生未定义的行为。

也就是说,尽管我们可以合法地为非事件的 union 成员形成一个左值(这就是为什么在没有构造的情况下分配给非事件成员是可以的),但它被认为是未初始化的。

关于c++ - 访问不活动的 union 成员和未定义的行为?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11373203/

相关文章:

c++ - 如何将 paintEvent 连接到插槽?

c++ - 未定义的行为和顺序点

c - 理解数组的概念

c++ - 谁在这里? g++ 还是 Visual Studio 2017?

c++ - "inline"关键字与 "inlining"概念

c++ - 为什么 std::pair 对 const 引用和转发引用参数有两个不同的构造函数?

c++ - 在 C 程序中解析 32 位整数

c++ - 带有 “auto”或模板类型推导的功能: “no matching function for call to…”

c++ - 使用 Detected Idiom 实现 is_destructible

C++ 最大 std::string 长度由堆栈大小或堆大小决定?