Le 12/08/2011 22:22, Joe Greer a écrit :
POD = Plain Old Data i.e. a struct or class with no special behavior in
the case.
Only you know what your app needs to do. I am just exploring
possibilities that you can either use or reject as suits your needs.
joe
AFAIK a POD may have special behavior. The only restriction is to have only
POD types as member variables, with the only base-non-POD types being
pointers
(and maybe arrays, but I'm not sure).
<<< Errors in, see after >>>
Roughly, it is a type whose member variables are PODs too.
But it may contain member functions, or not.
Some examples of POD and non-POD types :
struct POD { int i; }; // Only POD members
struct NonPOD { int * p; }; // Pointer isn't POD
struct DerivedPOD : POD { char c; }; // Only POD members
struct DerivedNonPOD : NonPOD { char c; }; // Inherits pointer, so not POD
struct DerivedFromBothNonPOD : POD, NonPOD { char c; }; // Idem
struct ComposedPOD { POD p; long l; }; // Only POD members
struct ComposedNonPOD { NonPOD p; long l; }; // At least one non-POD member
struct ComposedFromBothNonPOD { NonPOD p1; POD p2; long l; }; // Idem
struct MemberFunctionPOD { int i; int plus_one() { return ++i; } };
struct MemberFunctionNonPOD { int * p; int value() { return *p; } };
struct NonTrivialDestructorPOD {
int i;
~NonTrivialDestructorPOD() { i = 0; }
};
struct NonTrivialDestructorNonPOD {
int * i;
~NonTrivialDestructorNonPOD() { delete i; }
};
struct NonTrivialDestructorNonPOD2 {
int * i;
~NonTrivialDestructorNonPOD2 { *i = 0; }
};
Am I wrong anywhere ? <<< Yes, see below >>>
BTW, I just checked the latest free paper about C++0x, and I saw this.
(9.10)
A POD struct is a class that is both a trivial class and a standard-layout
class, and has no non-static data members of type non-POD struct, non-POD
union (or arrays of such types). Similarly, a POD union is a union that is
both a trivial class and a standard layout class, and has no non-static
data members of type non-POD struct, non-POD union (or arrays of such
types). A POD class is a class that is either a POD struct or a POD union.
So, as we are speaking of POD classes, we need :
- trivial
- standard-layout
- no non-static data members that are non-POD
Now, trivial classes.
(9.6)
A trivial class is a class that has a trivial default constructor (12.1)
and is trivially copyable.
[Note: In particular, a trivially copyable class does not have virtual
functions or virtual base classes. - end note]
So, we need :
- trivial default constructor
- trivially copyable
- no virtual functions
- no virtual base classes
- standard-layout
- no non-static data members that are non-POD
Now, trivially copyable.
(9.6)
A trivially copyable class is a class that:
- has no non-trivial copy constructors (12.8),
- has no non-trivial move constructors (12.8),
- has no non-trivial copy assignment operators (13.5.3, 12.8),
- has no non-trivial move assignment operators (13.5.3, 12.8),
- has a trivial destructor (12.4).
So, we need :
- trivial default, copy and move constructors
- trivial copy and move assignment operators
- trivial destructor
- no virtual functions, base classes
- standard-layout
- no non-static data members that are non-POD
Now, standard-layout.
(9.7)
A standard-layout class is a class that:
- has no non-static data members of type non-standard-layout class
(or arrays of such types) or reference,
- has no virtual functions (10.3) and no virtual base classes (10.1),
- has the same access control (Clause 11) for all non-static data
members,
- has no non-standard-layout base classes,
- 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
class with non-static data members, and
- has no base classes of the same type as the first non-static data
member.
So, we need :
- trivial default, copy and move constructors
- trivial copy and move assignment operators
- trivial destructor
- no virtual functions, base classes
- no non-static data members that are not standard-layout
- no virtual functions, base classes
- only one access control for all non-static data members
- no non-standard-layout base classes
- only one class in the hierarchy that has non-static data members
- no base classes of the same type as the first non-static data member
- no non-static data members that are non-POD
Restrictions 3 and 5 are the same.
Restriction 4 is a subset of restriction 10.
So, we need :
- trivial default, copy and move constructors
- trivial copy and move assignment operators
- trivial destructor
- no virtual functions, base classes
- only one access control for all non-static data members
- no non-standard-layout base classes
- only one class in the hierarchy that has non-static data members
- no base classes of the same type as the first non-static data member
- no non-static data members that are non-POD
So, we can do anything we want with static members and member functions.
So I've mistaken on examples NonPOD (pointer is POD), DerivedPOD
(restriction 7), ComposedNonPOD (NonPOD is POD, funny isn't it ?),
ComposedFromBothNonPOD (same reason), MemberFunctionNonPOD (pointer is
POD), NonTrivialDestructorPOD (restriction 3).
And NonTrivialDestructorNonPOD and NonTrivialDestructorNonPOD2 weren't
non-POD for the reason I thought.
Now, am I mistaking anywhere ?
Cheers & hope this article won't be too long to read,
Leo