gpt4 book ai didi

c++ - "there is no smaller array object that satisfies these constraints"是什么意思?

转载 作者:可可西里 更新时间:2023-11-01 15:19:44 33 4
gpt4 key购买 nike

draft n4659 for C++17 在第 4 章中描述了该语言的一般原理。在第 4.5 章中,C++ 对象模型 [intro.object],我无法理解一句话的意思(强调我的意思)

3 If a complete object is created (8.3.4) in storage associated with another object e of type “array of N unsigned char” or of type “array of N std::byte” (21.2.1), that array provides storage for the created object if:
(3.1) — the lifetime of e has begun and not ended, and
(3.2) — the storage for the new object fits entirely within e, and
(3.3) — there is no smaller array object that satisfies these constraints.

虽然示例表明数组可以为比数组短得多的元素提供存储空间:

struct A { unsigned char a[32]; };
struct B { unsigned char b[16]; };
A a;
B *b = new (a.a + 8) B; // a.a provides storage for *b
int *p = new (b->b + 4) int; // b->b provides storage for *p

这里 *p 在一个 16 字节的数组中只使用了 4 个字节(假设 sizeof(int) 是 4)。那么,3.3是什么意思呢?

最佳答案

if 3.3的意思是区分a[32]b[16]。前者不为 *p 提供存储,因为后者提供。它标识最小的唯一数组对象,提供对象所在的存储区域。

如果没有 3.3,定义将是可传递的。 a[32] 将为 *p 提供存储,因为它最终为 b[16] 提供存储。


关于 *p 使用 4 个字节。重要的是要注意 [b->b + 4, b->b +8) 区域虽然是 *p 所在的存储区,但不是数组提供存储的对象(该区域根本不是数组对象)。最小的数组对象是 b->b

关于c++ - "there is no smaller array object that satisfies these constraints"是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48075112/

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