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?
POD is an acronym in C++ that means plain old data. It is a class/struct that ONLY has member variables and no methods, constructors, destructors, virtual functions, etc.
In computer science and object-oriented programming, a passive data structure (PDS, also termed a plain old data structure, or plain old data, POD) is a term for a record, to contrast with objects.
“POD” is equivalent to “trivial and standard layout”, but for many code patterns, a narrower restriction to just “trivial” or just “standard layout” is appropriate; to encourage such precision, the notion of “POD” was therefore deprecated. The library trait is_pod has also been deprecated correspondingly.
To be more clear, a POD is what we call "a struct": a unit or a group of units that just store data.
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" );
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With