c++inheritance

What rules do I have to follow in order to ensure that a C++ type is POD?


Possible Duplicate:
What are POD types in C++?

What are Aggregates and PODs and how/why are they special?

I'm writing an interpreter in C++ and I want to make sure that certain C++ data types have a predictable layout for when they are accessed via interpreted code, particularly when using reflection. So for example, I want to make sure that the first data field is always at offset zero from the address of the object. Now, this is trivial for purely POD types. I'm wondering, however, if this can also work with objects that have inheritance or have constructors, as long as I avoid obvious things like virtual functions or multiple inheritance. Is it reasonable to assume that the compiler will layout these types the same way a C compiler would, or would the "unspecified behavior" in the C++ standard be something I would need to worry about?


Solution

  • c++11 defines the standard layout :

    Standard layout is intended to capture the first intent -- creating something with a layout the same as you'd get in C

    and this is what you are looking for. Therefore your check should be :

    static_assert( std::is_standard_layout<A>::value, "not standard layout" );