Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can a class with all private members be a POD class?

Tags:

c++

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; }
    };
}
like image 705
Billy ONeal Avatar asked Jan 21 '11 19:01

Billy ONeal


1 Answers

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

like image 72
Prasoon Saurav Avatar answered Sep 28 '22 21:09

Prasoon Saurav