简体   繁体   English

c ++ 11标准布局-使用相同的访问控制

[英]c++11 standard-layout - using same access control

I thought that the whole point of PODs (c++11, trivial + standard-layout) is to make sure the type is compatible with C.我认为 PODs (c++11, trivial + standard-layout) 的重点是确保类型与 C 兼容。

Given the following code:鉴于以下代码:

// that one is a standard layout, and trivial which makes it a c++11 POD
struct Bar
{
public:
  int x;
public:
  int y;
};

AFAIU, compiler might reorder x and y. AFAIU,编译器可能会重新排序 x 和 y。 Wouldn't that break compatibility with C?这不会破坏与 C 的兼容性吗?

Why that 98/03 POD definition relaxation in c++11 considered to be a good idea?为什么在 c++11 中放宽 98/03 POD 定义被认为是一个好主意?

AFAIU, compiler might reorder x and y. AFAIU,编译器可能会重新排序 x 和 y。 Wouldn't that break compatibility with C?这不会破坏与 C 的兼容性吗?

In C++03, it can.在 C++03 中,它可以。 In C++11 it cannot.在 C++11 中它不能。 C++11's standard layout rules only require that all of the members have the same access control. C++11 的标准布局规则要求所有成员具有相同的访问控制。 They don't have to be declared in the same access control region.它们不必在同一个访问控制区域中声明。

Why that 98/03 POD definition relaxation in c++11 considered to be a good idea?为什么在 c++11 中放宽 98/03 POD 定义被认为是一个好主意?

I think you're misunderstanding things.我认为你误解了一些事情。 The C++11 rules allow more types to be standard-layout (and thus potentially layout-compatible with C types), not less. C++11 规则允许更多类型成为标准布局(因此可能与 C 类型布局兼容),而不是更少。 Thus, there's no real downside to relaxing the rules.因此,放宽规则并没有真正的缺点。

I thought that the whole point of PODs (c++11, trivial + standard-layout) is to make sure the type is compatible with C.我认为 PODs (c++11, trivial + standard-layout) 的重点是确保类型与 C 兼容。

Not exactly the whole point of it, but yes, that is one of the properties of PODs.不完全是它的全部意义,但是,这是 POD 的特性之一。

 // that one is a standard layout, and trivial which makes it a c++11 POD

Correct.正确的。

AFAIU, compiler might reorder x and y. AFAIU,编译器可能会重新排序 x 和 y。 Wouldn't that break compatibility with C?这不会破坏与 C 的兼容性吗?

We already established it is a POD, which means the compiler will maintain compatibility with C. Maintaining compatibility with C does not break compatibility with C.我们已经确定它是一个 POD,这意味着编译器将保持与 C 的兼容性。保持与 C 的兼容性并不会破坏与 C 的兼容性。

Why that 98/03 POD definition relaxation in c++11 considered to be a good idea?为什么在 c++11 中放宽 98/03 POD 定义被认为是一个好主意?

Because it doesn't break anything.因为它不会破坏任何东西。

The point of POD is not just to make sure the type is compatible with C - note that a type with an access specifier ( public , private , etc.) is by definition not compatible with C since C doesn't have access specifiers. POD 的重点不仅仅是确保类型与 C 兼容 - 请注意,具有访问说明符( publicprivate等)的类型根据定义与 C 不兼容,因为 C 没有访问说明符。 The main property of a POD type is that it can be memcpy'ed around. POD 类型的主要属性是它可以被内存化。

Having more than one access specifier in a C++ type does permit the compiler to lay out the type in a non-specified way, and that's been true for a while (it's not new with C++11):在 C++ 类型中拥有多个访问说明符确实允许编译器以非指定的方式布置类型,这已经有一段时间了(这不是 C++11 的新内容):

From C++03 9.2/12从 C++03 9.2/12

Nonstatic data members of a (non-union) class declared without an intervening access-specifier are allocated so that later members have higher addresses within a class object.没有中间访问说明符声明的(非联合)类的非静态数据成员被分配,以便后面的成员在类对象中具有更高的地址。 The order of allocation of nonstatic data members separated by an access-specifier is unspecified (11.1).由访问说明符分隔的非静态数据成员的分配顺序未指定 (11.1)。

However, that doesn't make a type a non-POD - it can still be a POD, just not one that can be portably expressed in C.但是,这并不会使类型成为非 POD - 它仍然可以是 POD,只是不能用 C 可移植地表达。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM