tags:

views:

8086

answers:

4

I've been following SO for a bit now, and I've come across this term POD-type a few times... what does it mean?

+29  A: 

POD stands for Plain Old Data - that is, a struct (or class) with no members except data members. Wikipedia goes into a bit more detail and defines a POD in C++ as "A Plain Old Data Structure in C++ is an aggregate class that contains only PODS as members, has no user-defined destructor, no user-defined copy assignment operator, and no nonstatic members of pointer-to-member type."

Greg Hewgill
Hmmm... I guess I prefer more correct technical term of "intrinsic type" to this kind of slang. ;)
ceretullis
There's a difference. Intrinsic types are the "builtin" language primitives. POD types are these, plus aggregations of these (and other PODs).
Adam Wright
POD: int,char,float etc. The built in types.
Martin York
So PODs are c-style structs?
KitsuneYMG
@Greg Hewgill: Why do we need to differentiate between POD's and non-POD's at all?
Lazer
+2  A: 

Plain Old Data

in short it is all builtin data type (ex: int, char, float, long int, unsigned char, double) and all aggregation of POD data. Yes, it's a recursive definition ;)

To be more clear, a POD is what we call 'a struct'.

ugasoft
It's true that we sometimes call them 'a struct'. However we're always wrong to do so, since a struct is not necessarily a POD-type.
Steve Jessop
obviously... struct and class are almost equivalent, but in "the business" we call 'a struct' a simple data collector, usually without ctors and dtor, usually with value semantics...
ugasoft
+30  A: 

Very informally:

A POD is a type (including classes) where the C++ compiler guarantees that there will be no "magic" going on in the structure: for example hidden pointers to vtables, offsets that get applied to the address when it is cast to other types (at least if the target's POD too), constructors, or destructors. Roughly speaking, a type is a POD when the only things in it are built-in types and combinations of them. The result is something that "acts like" a C type.

Less informally:

  • int, char, wchar_t, bool, float, double are PODs, as are long/short and signed/unsigned versions of them.
  • pointers (including pointer-to-function and pointer-to-member) are PODs,
  • enums are PODs
  • a const or volatile POD is a POD.
  • a class, struct or union of PODs is a POD provided that all members are public, and it has no base class and no constructors, destructors, or virtual methods. Static members don't stop something being a POD under this rule.
  • Wikipedia is wrong to say that a POD cannot have members of type pointer-to-member. Or rather, it's correct for the C++98 wording, but TC1 made explicit that pointers-to-member are POD.

Here's what the C++ standard says:

3.9(10): "Arithmetic types (3.9.1), enumeration types, pointer types, and pointer to member types (3.9.2) and cv-qualified versions of these types (3.9.3) are collectively caller scalar types. Scalar types, POD-struct types, POD-union types (clause 9), arrays of such types and cv-qualified versions of these types (3.9.3) are collectively called POD types"

9(4): "A POD-struct is an aggregate class that has no non-static data members of type non-POD-struct, non-POD-union (or array of such types) or reference, and has no user-define copy operator and no user-defined destructor. Similarly a POD-union is an aggregate union that has no non-static data members of type non-POD-struct, non-POD-union (or array of such types) or reference, and has no user-define copy operator and no user-defined destructor.

8.5.1(1): "An aggregate is an array or class (clause 9) with no user-declared constructors (12.1), no private or protected non-static data members (clause 11), no base classes (clause 10) and no virtual functions (10.3)."

Steve Jessop
You have formal/less formal. You could add rule of thumb. Built in types and aggregations of Built in types (or something like that). In addition to get the exact definition we need to make the knowledge easy to use.
Martin York
I've added a "roughly speaking" clause. I don't want to say "aggregate" because it's a jargon term, and a casual reader wouldn't know that it means no virtual functions, no private members, etc.
Steve Jessop
Thanks for the feedback.
Steve Jessop
You're a bit wrong on the "offsets when _cast_to_ another type" bit.Those offsets are applied when casting to a base or derived class. So, if you cast from a POD base class pointer to a non-POD derived class, you may still encounter an adjustement.
MSalters
Good catch, thank you.
Steve Jessop
@Steve Jessop: Why do we need to differentiate between POD's and non-POD's at all?
Lazer
@Lazer: that's a whole other question, "how do PODs behave?" as opposed to "what does POD mean?". In summary the difference relates to initialisation (hence also use of memcpy to duplicate objects), compatibility with C struct layout for that compiler, and pointer up- and down-casting. PODs "act like C types", non-PODs aren't guaranteed to do so. So if you want your type to portably act like a C struct, you must ensure that it is POD, so you need to know the difference.
Steve Jessop
+1  A: 

With C++, Plain Old Data doesn't just mean that things like int, char, etc are the only types used. Plain Old Data really means in practice that you can take a struct memcpy it from one location in memory to another and things will work exactly like you would expect (i.e. not blow up). This breaks if your class, or any class your class contains, has as a member that is a pointer or a reference or a class that has a virtual function. Essentially, if pointers have to be involved somewhere, its not Plain Old Data.

Pointers are allowed in POD structs. References aren't.
j_random_hacker