有人能解释一下吗? 解决方案 你用来访问 y 的存储值的左值不是 *reinterpret_cast(&y),属于 Foo 类型,但它是 reinterpret_cast(&y)->x,其类型为 浮动.使用 float 类型的左值访问 float 没问题.在 C++ 中,您不能访问联合或结构的值"(作为整体),您只能访问单个成员.您引用的基本原理指出了 ​​C 和 C++ 之间的区别: struct X { int a, b;};struct X v1 = {1, 2}, v2;v2 = v1;在 C 中,标准规定赋值会加载 v1 的值(作为整体)以将其分配给 v2.这里对象 v1.a 和 v2.b(都具有 int 类型)的值使用 类型的左值访问struct X(不是 int).在 C++ 中,标准说赋值调用编译器生成的赋值运算符,相当于struct X {...结构X&运算符=(const struct X&other){a = 其他.a;b = 其他.b;}};在这种情况下,调用赋值运算符不会访问任何值,因为 RHS 是通过引用传递的.并执行赋值运算符分别访问两个 int 字段(这很好,即使没有聚合规则),因此这又不是通过 struct X.Previously, in basic.lval, there was this bullet point:In the current draft, it is gone.There is some background information at WG21's site: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2019/p1359r0.html#2051:Can anyone explain to me, what this means? What has this strict aliasing rule to do with struct assignment in C?cppreference says about this rule:I don't understand, why it is true. For example,struct Foo { float x;};float y;float z = reinterpret_cast<Foo*>(&y)->x;The last line seems to do what the bullet point describes. It accesses y (a float) through an aggregate, which includes a float (member x).Can anyone shed some light on this? 解决方案 The lvalue you use to access the stored value of y is not *reinterpret_cast<Foo*>(&y), of type Foo, but it is reinterpret_cast<Foo*>(&y)->x, which has the type float. Accessing a float using an lvalue of type float is fine. In C++, you can not "access the value of a union or struct" (as whole), you can only access individual members. The rationale you quoted points to a difference between C and C++: struct X { int a, b; }; struct X v1 = {1, 2}, v2; v2 = v1;In C, the standard says that the assignment loads the value of v1 (as whole) to assign it to v2. Here the values of the objects v1.a and v2.b (both have types int) are accessed using an lvalue of type struct X (which is not int).In C++, the standard says that the assignment calls the compiler generated assignment operator which is equivalent tostruct X { ... struct X& operator=(const struct X&other) { a = other.a; b = other.b; }};In this case, calling the assignment operator does not access any value, because the RHS is passed by reference. And executing the assignment operator accesses the two int fields separately (which is fine, even without the aggregate rule), so this is again not accessing a value through an lvalue of type struct X. 这篇关于“包含上述类型之一的聚合或联合类型"发生了什么?严格的别名规则?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
07-25 04:26