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

I heard that POD types cannot have personal data, but according to the C ++ 0x project, I have a weaker requirement (my highlight):

has the same access control (section 11) for all non-static data members

which, apparently, assumes that private data is in order, while all are private. I don't have a copy of C ++ 03, although for verification ...

Will WindowsApi::Uuid be a POD class?

 namespace WindowsApi { class Uuid { union { ::UUID asUuid; //Win32 UUID struct unsigned __int64 asInt64s[2]; unsigned __int32 asInt32s[4]; }; public: Uuid() {} Uuid(::UUID sourceStructure) : asUuid(sourceStructure) {} operator ::UUID() { return asUuid; } }; } 
+4
source share
3 answers

C ++ 03 still does not allow non-static private or protected data in POD classes. This requirement is specified in the unit definition.

An aggregate is an array or class (section 9) without any user-declared constructors (12.1), without private or protected non-static data elements (section 11) , without base classes (clause 10), and there are no virtual functions (10.3).

and the POD class must first be an aggregate.

+9
source

I heard that POD types cannot have personal data

In C ++ 03, POD types cannot have personal data (see AndreyT answer).

However, the definition of POD has been changed in C ++ 0x (see 9/10 ).

By n3225

A POD structure is a class that is both a trivial class and a standard layout class and does not have non-static data members such as non-POD struct, non-POD union (or an array of such types).
......

The POD class is a class that is either a POD structure or a POD union.

It means

 struct demo { private: int a, b; }; 

is a POD in C ++ 0x because demo is a trivial and standard layout .

The definition of the standard layout is in section 9/7

A standard layout class is a class that:

  • does not have non-static data members such as a non-standard class layout (or an array of such types) or a link,
  • does not have virtual functions (10.3) and there are no virtual base classes (10.1),
  • has the same access control (section 11) for all non-static data members ,
  • does not have base classes of non-standard layout,
  • either does not have non-static data elements in the derived class itself, but no more than one base class with non-static data elements or does not have base classes with non-static data elements and
  • does not have base classes of the same type as the first non-static data element. 11

.

Will WindowsApi :: Uuid then be a POD class?

Nopes! WindowsApi::Uuid is neither a 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 copied. WindowsApi::Uuid has a non-trivial default constructor.

So this rule has become relaxed in C ++ 0x, then?

Yes! (Subject to paragraph 11)

Also check Frequently Asked Questions for Aggregates and PODs

+8
source

According to my n3225 C ++ 0x draft, WindowsApi::Uuid is a POD class.

From page 219: A POD structure is a class that is a trivial class and a standard layout class, and does not have non-static data members such as non-POD struct, non-POD union (or an array of such types).

A trivial class is a class that has a trivial default constructor and can be trivially copied:

A trivially-copied class is a class that:

  • does not have non-trivial copy constructors (12.8),
  • has no non-trivial displacement constructors (12.8),
  • no nontrivial copy assignment operators (13.5.3, 12.8),
  • has no non-trivial displacement assignment operators (13.5.3, 12.8) and
  • has a trivial destructor (12.4).

A standard layout class is a class that:

  • does not have non-static data members such as a non-standard class layout (or an array of such types) or a link,
  • does not have virtual functions (10.3) and there are no virtual base classes (10.1),
  • has the same access control (section 11) for all non-static data members ,
  • does not have base classes of non-standard layout,
  • either does not have non-static data members in the derived class and no more than one base class with non-static data members, or does not have base classes with non-static data members, and
  • does not have base classes of the same type as the first non-static data element.

Since WindowsApi does not violate any of these restrictions, it will be a valid POD class in C ++ 0x. As AndreyT mentions, this is a more generous formulation than C ++ 03.

-1
source

Source: https://habr.com/ru/post/1336513/


All Articles