C++ Standard Layout and References
Asked Answered
P

1

7

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?

Paresh answered 13/4, 2013 at 23:24 Comment(2)
Are you presuming such properties need exist? There need not be...Petroleum
"Prevent" is a strong word. From one point of view, references just give the compiler a good excuse to treat the struct as an abstract data type rather than a. storage layout specification. References don't have to have any storage of their own at all.Arak
A
12

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 ]

Abyssal answered 13/4, 2013 at 23:33 Comment(1)
Thanks for the great answer: that makes perfect sense.Paresh

© 2022 - 2024 — McMap. All rights reserved.