1

According to the current C++ standard draft, a standard-layout class

either has no non-static data members in the most derived class and at most one base class with non-static data members, or has no base classes with non-static data members

I have yet to see any implementation that would be more efficient with this limitation. Why does it exist (except for making things more difficult)?

Hristo Venev
  • 972
  • 5
  • 17
  • 2
    Where does that text come from? Please provide some context. – Some programmer dude Mar 26 '14 at 13:42
  • This question is more like: Is there an implementation where the given limitation would make any sense? – Hristo Venev Mar 26 '14 at 13:44
  • @HristoVenev I look at POD structures this way: its just data, and that means it can be easily copied across boundaries. A simple way to think of it is I can write this structure (and an array of this structure) in its entirety to a file super easy (simple memory dump). If it has static data members, or has multiple inheritance, that is non-trivial: where do I put that static data member in the file? I now have to do fancy parsing to get that. POD structures are easier to use in many cases because of their memory layout. – IdeaHat Mar 26 '14 at 13:56
  • `struct A{int a;};struct B:A{int b;};` B can be considered standard-layout in most implementations. – Hristo Venev Mar 26 '14 at 14:01
  • @JoachimPileborg That's from the `std::is_standard_layout` docs. I just edited the post to clarify. – cf- Mar 26 '14 at 14:18
  • It's from the current standard draft. – Hristo Venev Mar 26 '14 at 14:19
  • `is_standard_layout` is about interoperability, not efficiency. Basically standard layout objects are C structs. In your example it is not specified whether `a` is located before or after `b` in the memory. A C struct has a well-defined order of fields, your object does not. The standard would not dictate inheritance layout to implementations. – n. m. could be an AI Mar 26 '14 at 14:27
  • @n.m. Why would the order be undefined in this case? I mean, why would an implementation not define it as 'base class goes first'? – Hristo Venev Mar 26 '14 at 14:28
  • mainly because diamond shaped inheritance – sp2danny Mar 26 '14 at 14:55

1 Answers1

0

The purpose of standard-layout is not efficiency, its data-interoperability with C

sp2danny
  • 7,488
  • 3
  • 31
  • 53
  • So if I want to use a C library that requires me to use `offsetof` for something but lets me define my own structs, I can't use inheritance. – Hristo Venev Mar 26 '14 at 14:26
  • If you want to 'send' a struct to a C library, you have to be able to declare it in C++ somehow, so that the layout is what the lib expects – sp2danny Mar 26 '14 at 14:30
  • I declare it as follows: size; offsets to special members – Hristo Venev Mar 26 '14 at 14:33