9

I've heard before that POD types cannot have private data -- but according to the C++0x draft I have the requirement is looser (emphasis 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; }
    };
}
Jonas
  • 121,568
  • 97
  • 310
  • 388
Billy ONeal
  • 104,103
  • 58
  • 317
  • 552

3 Answers3

13

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

Community
  • 1
  • 1
Prasoon Saurav
  • 91,295
  • 49
  • 239
  • 345
11

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.

AnT stands with Russia
  • 312,472
  • 42
  • 525
  • 765
  • So this rule got relaxed in C++0x then? (I don't see anything about aggregates in the C++0x draft around the definition of POD types) – Billy ONeal Jan 21 '11 at 19:15
-1

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.

Bill
  • 14,257
  • 4
  • 43
  • 55
  • I don't see any user-defined copy constructor in that class. Do the constructor and the conversion operator combine to form a copy constructor? – Rob Kennedy Jan 21 '11 at 19:49
  • It does not have a user defined copy constructor. `::WindowsApi::Uuid` is my class, `::UUID` is the one defined by Windows. (Note the CaPs difference) – Billy ONeal Jan 21 '11 at 19:49
  • @Billy : You have a non trivial default constructor though. `WindowsApi::Uuid` is not a POD. – Prasoon Saurav Jan 21 '11 at 20:09
  • @Billy: Thanks! Somehow I misread ::UUID as the name of the class. I'll update. @Prasoon: A default constructor is one that can be called without any parameters. The only defined constructor isn't one. – Bill Jan 21 '11 at 23:27