Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 363
Talin Avatar asked Nov 05 '12 07:11

Talin


People also ask

What is a POD type C++?

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.

What is POD programming?

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.

Why is Is_pod deprecated?

“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.

Is a struct a POD?

To be more clear, a POD is what we call "a struct": a unit or a group of units that just store data.


1 Answers

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" );
like image 93
BЈовић Avatar answered Oct 13 '22 21:10

BЈовић