一个对象可以有多个有效类型吗? [英] Can an object have more than one effective type?

查看:156
本文介绍了一个对象可以有多个有效类型吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在ABI不会在联合中插入填充的平台上考虑以下代码:

Consider the following code on a platform where the ABI does not insert padding into unions:

union { int xi; } x;
x.xi = 1;

我相信第二行表现出未定义的行为,因为它违反了严格的别名规则:

I believe that the second line exhibits undefined behaviour as it violates the strict aliasing rule:

  1. x.xi引用的对象与x引用的对象相同.两者都是相同的存储区域,术语 object 在ISO 9899:2011§3.15中定义为:

  1. The object referred to by x.xi is the same object as the object referred to by x. Both are the same region of storage and the term object is defined in ISO 9899:2011 §3.15 as:

对象

执行环境中数据存储的1 区域,其内容可以表示值

object

1 region of data storage in the execution environment, the contents of which can represent values

2& NOTE 被引用时,对象可以解释为具有特定类型;参见6.3.2.1.

2 NOTE When referenced, an object may be interpreted as having a particular type; see 6.3.2.1.

由于一个对象只不过是一个存储区域,所以我得出结论,由于xx.xi占用相同的存储空间,因此它们是同一对象.

As an object is not more than a region of storage, I conclude that as x and x.xi occupy the same storage, they are the same object.

x有效类型union { int xi; },因为它是使用它声明的类型.参见§6.5¶6:

The effective type of x is union { int xi; } as that's the type it has been declared with. See §6.5 ¶6:

6  有效类型的对象用于访问其存储的值是该对象的声明的类型(如果有的话). 87) 如果将值存储在一个对象中通过一个非字符类型的左值没有声明的类型,则左值的类型成为该访问和不修改存储值的后续访问的对象的有效类型.如果使用memcpymemmove将值复制到没有声明类型的对象中,或者将其复制为字符类型数组,则该访问和未修改的后续访问的已修改对象的有效类型值是从中复制值的对象的有效类型(如果有).对于所有其他没有声明类型的对象的访问,该对象的有效类型就是用于访问的左值的类型.

6  The effective type of an object for an access to its stored value is the declared type of the object, if any.87) If a value is stored into an object having no declared type through an lvalue having a type that is not a character type, then the type of the lvalue becomes the effective type of the object for that access and for subsequent accesses that do not modify the stored value. If a value is copied into an object having no declared type using memcpy or memmove, or is copied as an array of character type, then the effective type of the modified object for that access and for subsequent accesses that do not modify the value is the effective type of the object from which the value is copied, if it has one. For all other accesses to an object having no declared type, the effective type of the object is simply the type of the lvalue used for the access.

87) 分配的对象没有声明的类型.

87) Allocated objects have no declared type.

通过¶6的措辞,很明显每个对象只能有一个有效的类型.

By the wording of ¶6 it is also clear that each object can only have one effective type.

在语句x.xi中,我通过键入int的左值x.xi访问x.这不是§6.5¶7中列出的类型之一:

In the statement x.xi I access x through the lvalue x.xi typed int. This is not one of the types listed in §6.5 ¶7:

7 对象只能通过具有以下类型之一的左值表达式访问其存储值: 88)

  • 与对象的有效类型兼容的类型
  • 与对象的有效类型兼容的类型的限定版本,
  • 一种类型,它是与有效类型相对应的有符号或无符号类型 对象
  • 一种类型,它是与标准版本对应的有符号或无符号类型 对象的有效类型,
  • 在其中包括上述类型之一的集合或联合类型 成员(递归地包括子集合或所包含的联盟的成员),或
  • 一种字符类型.
  • a type compatible with the effective type of the object,
  • a qualified version of a type compatible with the effective type of the object,
  • a type that is the signed or unsigned type corresponding to the effective type of the object,
  • a type that is the signed or unsigned type corresponding to a qualified version of the effective type of the object,
  • an aggregate or union type that includes one of the aforementioned types among its members (including, recursively, a member of a subaggregate or contained union), or
  • a character type.

88)此列表的目的是指定对象可能会别名也可能不会别名的那些情况.

88) The intent of this list is to specify those circumstances in which an object may or may not be aliased.

  • 因此,第二行表现出不确定的行为.

  • Therefore, the second line exhibits undefined behaviour.

    由于这种解释显然是错误的,我对标准的误解在哪里?

    As this interpretation is clearly wrong, where lies my misreading of the standard?

    推荐答案

    错误认为xx.xi是同一对象.

    The error is thinking that x and x.xi are the same object.

    联合是一个对象,它包含成员对象 1 .它们是不同的对象,每个对象都有其自己的类型.

    The union is an object and it contains member objects1. They are distinct objects, each with it's own type.

    1.(引自:ISO/IEC 9899:20x1 6.2.5 Types 20)
    联合类型描述了一个重叠的非空成员对象集,每个成员对象 其中有一个可选的名称和可能不同的类型.

    1. (Quoted from: ISO/IEC 9899:20x1 6.2.5 Types 20)
    A union type describes an overlapping nonempty set of member objects, each of which has an optionally specified name and possibly distinct type.

    这篇关于一个对象可以有多个有效类型吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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