Can a class with all private members be a POD class?
I've heard before that POD types cannot have private data -- but according to the C++0x draft I have the requirement is looser (emphasi开发者_StackOverflow中文版s mine):
has the same access control (Clause 11) for all non-static data members
which seems to suggest that private data is okay so long as it's all private. I don't have a copy of C++03 though to check...
Would then, WindowsApi::Uuid
be a POD class?
namespace WindowsApi
{
class Uuid
{
union
{
::UUID asUuid; //Win32's UUID struct
unsigned __int64 asInt64s[2];
unsigned __int32 asInt32s[4];
};
public:
Uuid() {}
Uuid(::UUID sourceStructure) : asUuid(sourceStructure) {}
operator ::UUID() { return asUuid; }
};
}
I've heard before that POD types cannot have private data
In C++03 POD types cannot have private data (see AndreyT's answer).
However the definition of POD has been changed in C++0x (See 9/10
).
As per n3225
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 array of such types).
... ...A POD class is a class that is either a POD struct or a POD union.
That means
struct demo
{
private:
int a, b;
};
is POD in C++0x because demo
is both trivial and standard layout.
The definition of Standard layout is in section 9/7
A standard-layout class is a class that:
- has no non-static data members of type non-standard-layout class (or array 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 classes with non-static data members, and
- has no base classes of the same type as the first non-static data member.11
.
Would then, WindowsApi::Uuid be a POD class?
Nopes! WindowsApi::Uuid
is neither POD in C++03 nor in C++0x. A trivial class is a class that has a trivial default constructor (12.1) and is trivially copyable. WindowsApi::Uuid
has a non trivial default constructor.
So this rule got relaxed in C++0x then?
Yes! (Considering Clause 11)
Also check out the FAQ entry on Aggregates and PODs
C++03 still does not allow non-static private or protected data in POD classes. This requirement is specified in the definition of aggregate
An aggregate is an array or a 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).
and POD class must be an aggregate first.
According to my n3225 C++0x draft, WindowsApi::Uuid
is a POD class.
From page 219: 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 array of such types).
A trivial class is a class that has a trivial default constructor and is trivially copyable:
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), and
- has a trivial destructor (12.4).
A standard-layout class is a class that:
- has no non-static data members of type non-standard-layout class (or array 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 classes with non-static data members, and
- has no base classes of the same type as the first non-static data member.
Since WindowsApi
doesn't violate any of these constraints, it will be a valid POD class under C++0x. As AndreyT mentions, this is a more generous wording than C++03.
精彩评论