简体   繁体   English

C++ 标准布局和参考

[英]C++ Standard Layout and References

According to the C++ standard:根据 C++ 标准:

A standard-layout class is a class that:标准布局类是这样一个类:

—has no non-static data members of type non-standard-layout class (or array of such types) or reference. — 没有非标准布局类(或此类类型的数组)或引用类型的非静态数据成员。

What property(ies) of references prevent classes with reference members from being included in the definition of a standard layout class?引用的哪些属性会阻止具有引用成员的类包含在标准布局类的定义中?

A standard layout class is all about having a well defined layout for a particular type in memory .标准布局类就是为内存中的特定类型定义明确的布局。 In C++, references aren't objects so don't have any storage that can be accessed in a well defined way by a conforming program even though the implementation will usually have to have some sort of implementation specific storage for them.在 C++ 中,引用不是对象,因此没有任何存储可以由符合标准的程序以明确定义的方式访问,即使实现通常必须为它们提供某种特定于实现的存储。

For this reason it doesn't make sense to have reference members in something that must have a standard layout.出于这个原因,在必须具有标准布局的内容中使用引用成员是没有意义的。

There's a non-normative note in the standard in the section about the C++ memory model that mentions this:在关于 C++ 内存模型的部分的标准中有一个非规范性注释,提到了这一点:

[ Note: Various features of the language, such as references and virtual functions, might involve additional memory locations that are not accessible to programs but are managed by the implementation. [注意:该语言的各种特性,例如引用和虚拟函数,可能涉及程序无法访问但由实现管理的额外内存位置。 —end note ] ——尾注]

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

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