我需要验证我可以以某种方式使用 union 。
对于 C99,这个答案就足够了: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.
谁能帮我找到 C++ 的等效保证?
更一般地说,是否有一个很好的来源来查找 C++ 标准的章节/段落引用,这些引用等同于 C 中的已知功能?
非常感谢。
最佳答案
在 C++ (14882:2003) 中,这分为两段:
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.
我还没有听说过 C 和 C++ 之间有这样的交叉引用,它可能很重要,正如这个例子所示。
关于c++ - 我如何比较 C/C99 规范和 C++ 规范?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5246389/