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

Does casting to an unrelated reference type violate the strict aliasing rule?

严格的别名规则说

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)

我想知道下面的程序是否已经包含未定义的行为,是否存在 "an access to the stored value":

#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

由于您的程序不会尝试执行上述操作,因此不会违反严格的别名。但是,尝试 使用 那些 pointers/references 来读取或写入将是违规行为。所以这是一条很好的路线。但是仅获得 references/pointers 并不违反您所说的第一段。转换为不相关的 reference/pointer 类型只处理对象的 identity/address,而不是它的值。