我认为 POD(c++11,琐碎 + 标准布局)的全部意义在于确保类型与 C 兼容。
给定以下代码:
// that one is a standard layout, and trivial which makes it a c++11 POD
struct Bar
{
public:
int x;
public:
int y;
};
AFAIU,编译器可能会重新排序 x 和 y。这不会破坏与 C 的兼容性吗?
为什么 c++11 中的 98/03 POD 定义放宽被认为是个好主意?
最佳答案
AFAIU, compiler might reorder x and y. Wouldn't that break compatibility with C?
在 C++03 中,它可以。在 C++11 中它不能。 C++11 的标准布局规则仅要求所有成员都具有相同的访问控制。它们不必在同一访问控制区域中声明。
Why that 98/03 POD definition relaxation in c++11 considered to be a good idea?
我认为你误解了一些事情。 C++11 规则允许更多 类型为标准布局(因此可能与 C 类型布局兼容),而不是更少。因此,放宽规则并没有真正的坏处。
关于c++11 standard-layout - 使用相同的访问控制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12416015/