gpt4 book ai didi

c++ - 转换为不相关的引用类型是否违反了严格的别名规则?

转载 作者:搜寻专家 更新时间:2023-10-31 02:01:52 24 4
gpt4 key购买 nike

严格的别名规则说

If a program attempts to access the stored value of an object through a glvalue of other than one of the following types the behavior is undefined:

— the dynamic type of the object,

— a cv-qualified version of the dynamic type of the object,

— a type similar (as defined in 4.4) to the dynamic type of the object,

— a type that is the signed or unsigned type corresponding to the dynamic type of the object,

— a type that is the signed or unsigned type corresponding to a cv-qualified version of the dynamic type of the object,

— an aggregate or union type that includes one of the aforementioned types among its elements or nonstatic data members (including, recursively, an element or non-static data member of a subaggregate or contained union)

我想知道下面的程序是否已经包含未定义的行为,以及是否存在“对存储值的访问”:

#include <cstdint>

void foo() {
std::int32_t i = 1;
float f = 1.0f;

std::int32_t& r = reinterpret_cast<std::int32_t&>(f);

std::int32_t* p = reinterpret_cast<std::int32_t*>(&f);
}

据我所知,将浮点指针转换为 int 引用等同于`*reinterpret_cast(&x):

A glvalue expression of type T1 can be cast to the type “reference to T2” if an expression of type “pointer to T1” can be explicitly converted to the type “pointer to T2” using a reinterpret_cast The result refers to the same object as the source glvalue, but with the specified type. [ Note: That is, for lvalues, a reference cast reinterpret_cast(x) has the same effect as the conversion *reinterpret_cast(&x) with the built-in & and * operators (and similarly for reinterpret_cast(x)). —end note ]

对于指针,reinterpret_cast 归结为转换为 void*,然后转换为目标类型:

An object pointer can be explicitly converted to an object pointer of a different type.72 When a prvalue v of object pointer type is converted to the object pointer type “pointer to cv T”, the result is static_cast(static_cast(v)).

两个静态转换的语义定义为:

A prvalue of type “pointer to cv1 void” can be converted to a prvalue of type “pointer to cv2 T,” where T is an object type and cv2 is the same cv-qualification as, or greater cv-qualification than, cv1. The null pointer value is converted to the null pointer value of the destination type. If the original pointer value represents the address A of a byte in memory and A satisfies the alignment requirement of T, then the resulting pointer value represents the same address as the original pointer value, that is, A. The result of any other such pointer conversion is unspecified.

由于 int32_tfloat 具有相同的大小和对齐方式,我应该得到一个指向相同地址的新指针。我想知道的是,如果

a reference cast reinterpret_cast(x) has the same effect as the conversion *reinterpret_cast(&x) with the built-in & and * operators

已经构成对存储值的访问,或者如果必须稍后在某处进行访问以违反严格的别名规则。

最佳答案

您所做的转换不是对对象 if 的访问。

3.1 access [defns.access]

⟨execution-time action⟩ to read or modify the value of an object

由于您的程序不会尝试执行上述操作,因此它不会违反严格的别名。然而,尝试使用那些指针/引用来读取或写入将是一种违规行为。所以这是一条很好的路线。但是仅获取引用/指针并不违反您所说的第一段。转换为不相关的引用/指针类型仅处理对象的标识/地址,而不处理其值。

关于c++ - 转换为不相关的引用类型是否违反了严格的别名规则?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58329972/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com