gpt4 book ai didi

c++ - 如何通过聚合访问对象的存储

转载 作者:IT老高 更新时间:2023-10-28 23:01:20 26 4
gpt4 key购买 nike

在“Lvalues and rvalues”, [basic.lval] (3.10) 中,C++ 标准包含一个类型列表,因此通过这种类型的左值“访问对象的存储值”是有效的 (第 10 段)。具体来说,它说:

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,

  • [some unimportant details about CV and signed/unsigned]

  • 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),

  • [some more stuff]

“聚合”规则究竟是什么意思?如何通过一些通用聚合类型的 glvalue 访问对象的存储值?!

我正在想象这样的事情:

int a = 10;                                      // my "stored value"

struct Foo { char x; float y; int z; bool w; }; // an aggregate

reinterpret_cast<Foo&>(a).y = 0; // ???

最终的转换是否会产生“包含 a 的动态类型的聚合类型”的左值,从而使其有效?

最佳答案

该列表的目的不是为您提供访问对象的替代方法,而是如列表脚注所示,列出对象可能被别名的所有方式。考虑以下示例:

struct foo
{
char x;
float y;
int z;
bool w;
};

void func( foo &F, int &I, double &D )
{
//...
}

该列表的意思是访问 F也可以访问与访问 I 相同的底层对象。 .如果您传递了对 F.z 的引用,则可能会发生这种情况。为I ,像这样:

func(F, F.z, D); 

另一方面,您可以放心地假设无法访问 F访问与 D 相同的底层对象, 因为 struct foo不包含 double 类型的任何成员.

即使某些 clown 这样做也是如此:

union onion
{
struct foo F;
double D;
};

onion o;
int i;

func( o.F, i, o.D ); // [class.union] (9.5) wants a word with you. UB.

我不确定 union是您问题的核心。但是 union 之前的部分示例突出显示了聚合规则存在的原因。

现在让我们考虑您的示例:reinterpret_cast<Foo&>(a).y = 0; [expr.reinterpret.cast] (5.2.10),第 11 段有这样说:

An lvalue 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. That is, a reference cast reinterpret_cast<T&>(x) has the same effect as the conversion *reinterpret_cast<T*>(&x) with the built-in & and * operators (and similarly for reinterpret_cast<T&&>(x)). The result refers to the same object as the source lvalue, but with a different type. The result is an lvalue for an lvalue reference type or an rvalue reference to function type and an xvalue for an rvalue reference to object type. No temporary is created, no copy is made, and constructors (12.1) or conversion functions (12.3) are not called.71


71 This is sometimes referred to as a type pun.

在您的示例中,如果将指针转换为- int 是合法的指向-Foo的指针, 然后你的 reinterpret_cast<Foo&)(a)是合法的并产生一个左值。 (第 1 段告诉我们它将是一个左值。)而且,当我读到它时,根据第 7 段,指针转换本身是可以的:

A pointer to an object can be explicitly converted to a pointer to a different object type. When a prvalue v of type “pointer to T1” is converted to the type “pointer to cv T2”, the result is static_cast<cv
T2*>(static_cast<cv void*>(v))
if both T1 and T2 are standard-layout types (3.9) and the alignment requirements of T2 are no stricter than those of T1. Converting a prvalue of type “pointer to T1” to the type “pointer to T2” (where T1 and T2 are object types and where the alignment requirements of T2 are no stricter than those of T1) and back to its original type yields the original pointer value. The result of any other such pointer conversion is unspecified.

您拥有具有兼容对齐约束的标准布局类型。因此,您所拥有的是一种产生左值的双关语类型。您列出的规则本身并没有使其未定义。

那么什么可能使它未定义?好吧,首先,[class.mem] (9.2) 第 21 段提醒我们,指向标准布局结构对象的指针指向其初始成员,反之亦然。因此,在您输入双关语之后,您会看到对 Foo 的引用。 ,这样 Fooxa 位于同一位置.

而且...这就是我的语言律师逐渐消失的地方。我凭直觉知道访问 Foo通过该 franken-reference 是在 best 实现定义或未指定。我找不到它被明确地放逐到 undefined 行为领域的地方。

但是,我想我回答了您最初的问题:为什么存在聚合规则?它为您提供了一种非常基本的方法来判断潜在的别名,而无需进一步的指针分析。

关于c++ - 如何通过聚合访问对象的存储,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20275322/

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