Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ Standard Layout and References

According to the C++ standard:

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?

like image 626
TRISAbits Avatar asked Apr 13 '13 23:04

TRISAbits


People also ask

What is a standard layout?

A standard-layout type is a type with a simple linear data structure and access control that can easily be used to communicate with code written in other programming languages, such as C, either cv-qualified or not. This is true for scalar types, standard-layout classes and arrays of any such types.

Are references available in C?

No, it doesn't.

What does trivial mean in C++?

Trivial typesWhen a class or struct in C++ has compiler-provided or explicitly defaulted special member functions, then it is a trivial type. It occupies a contiguous memory area. It can have members with different access specifiers. In C++, the compiler is free to choose how to order members in this situation.


Video Answer


1 Answers

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.

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:

[ 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 ]

like image 154
CB Bailey Avatar answered Sep 22 '22 10:09

CB Bailey