我如何将 C/C99 规范与 C++ 进行比较规格?

发布于 2024-10-21 07:22:22 字数 392 浏览 3 评论 0原文

我需要验证我是否可以以某种方式使用联合体。

对于 C99,这个答案就足够了:联合元素对齐

C99 - 第 6.7.2.1 节结构和 union 说明符(第 14 段):

适当地,指向联合对象的指针 转换后,指向其每个 成员(或者如果成员是位字段, 然后到其所在单位), 反之亦然。

有人可以帮我找到 C++ 的等效保证吗?

更一般地说,是否有一个好的来源可以查找与 C 中已知功能等效的 C++ 标准的章节/段落引用?

多谢。

I need to verify that I can use unions a certain way.

For C99, this answer is adequate: Union element alignment

C99 - section 6.7.2.1 Structure and
union specifiers (paragraph 14):

A pointer to a union object, suitably
converted, points to each of its
members (or if a member is a bitfield,
then to the unit in which it resides),
and vice versa.

Can someone please help me find the equivalent guarantee for C++?

More generally, is there a good source for finding section/paragraph citations for C++ standards which are equivalent to a known feature in C?

Thanks a lot.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

执笏见 2024-10-28 07:22:22

在 C++ (14882:2003) 中,这分为两个段落:

9.5[class.union]/1

每个数据成员的分配就好像它
是结构体的唯一成员。

9.2[class.mem]/17

指向 POD 结构对象的指针,
使用适当转换
reinterpret_cast,指向它的
初始成员(或者如果该成员是位字段,则为它所在的单元),反之亦然。

我还没有听说过 C 和 C++ 之间有这样的交叉引用,它可能很重要,如本例所示。

In C++ (14882:2003), this is split between two paragraphs:

9.5[class.union]/1

Each data member is allocated as if it
were the sole member of a struct.

9.2[class.mem]/17

A pointer to a POD-struct object,
suitably converted using a
reinterpret_cast, points to its
initial member (or if that member is a bit-field, then to the unit in which it resides) and vice versa.

I haven't heard of a cross-reference between C and C++ like that, it may be non-trivial, as this example shows.

老娘不死你永远是小三 2024-10-28 07:22:22

仅供参考,如果您想检查 C++ 标准,不幸的是它不是免费的。您必须付费才能获得该标准的副本。不过,最终草案(批准前的草案)可在线获取:对于 C++99 http ://www.kuzbass.ru:8086/docs/isocpp/,对于 C++0x http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2010/n3092.pdf

FYI, if you want to check the C++ standard, unfortunately it is not available for free. You must pay a fee to get a copy of the standard. However, the final draft (the one before the approval) is available online: for C++99 http://www.kuzbass.ru:8086/docs/isocpp/, for C++0x http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2010/n3092.pdf.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文