2

By definition here, POD is a simple class with no user-defined constructors, non static members, and containing simple data types only.

The question is, will these 2 classes below be equivalent as POD types (in terms of memory footprint):

class pod
{
public:
   int x;
   double y;
};

class pod1
{
public:
   int x;
   double y;

   enum POD_TYPE
   {
      POD1 = 0,
      POD = 1
   };
};

In other words does adding enum to the class only affects scope resolution of enum and does not affect properties of the class itself? By observation, it seems that class is still pod, but I would like to confirm based on the standard.

Community
  • 1
  • 1
Ilya Kobelevskiy
  • 5,245
  • 4
  • 24
  • 41

3 Answers3

3

Yes, that type is still POD. The definition is given by C++11 9/10:

A POD struct is a non-union 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).

Trivial means that it doesn't do any funny business when creating, destroying or copying objects. Standard-layout means that it doesn't do any funny business with the layout of data members: no polymorphism, and restrictions on what you can do with access specifiers and inheritance. These terms are fully defined in C++11 9/6 and 9/7, if you want more detail.

Nested types (such as your enumeration), static data members and non-virtual member functions (apart from constructors etc. which would make it non-trivial) will not effect any of those things, so it is still POD.

UPDATE: Since you say you're interested in historic definitions, C++03 defined:

9/4 A POD-struct is an aggregate class that has no non-static members of type non-POD-struct, non-POD-union (or array of such types), and has no user-defined copy assignment operator and no user-defined destructor"

8.5.1/1 An aggregate is an array or class with no user-declared constructors, no private or protected non-static data members, no base classes and no virtual functions.

So there were more restrictions; but nested types were still allowed. I don't have a copy of C++98, but I'm sure that would be identical to C++03.

Community
  • 1
  • 1
Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
  • Thanks, that is a very comprehensive summary! You mention C++11, which relaxes the conditions for POD - would your answer be still applicable for regular C++ (part regarding nested types)? – Ilya Kobelevskiy Oct 25 '13 at 12:55
  • @IlyaKobelevskiy: The short answer is yes. I've updated my answer to include historic definitions. – Mike Seymour Oct 25 '13 at 13:05
2

It doesn't make any difference regarding POD status because defining a nested enum does not add data members to the class. In fact, you could also define a nested class that is not POD inside pod1 and it would still not make a difference regarding the PODness of pod1.

Jon
  • 428,835
  • 81
  • 738
  • 806
  • Thanks, that is what I thought too! But, does the standard explicitly states that (for regular C++, for C++11 it does by @Mike Seymour answer), or it is just common thing for most compiler implementations? – Ilya Kobelevskiy Oct 25 '13 at 13:00
1

The enum POD_TYPE is a type and won't effect the layout, we can see this from the draft C++ standard section 9.2 Class members paragraph 1 which says:

[...]Members of a class are data members, member functions (9.3), nested types, and enumerators. Data members and member functions are static or non-static; see 9.4. Nested types are classes (9.1, 9.7) and enumerations (7.2) [...]

as opposed to data members and we can further see that the definition of a standard layout class depends only on data member from paragraph 16 which says:

Two standard-layout struct (Clause 9) types are layout-compatible if they have the same number of non-static data members and corresponding non-static data members (in declaration order) have layout-compatible types (3.9).

and we further see going back to section 9 Classes paragraph 10 which says(emphasis mine):

A POD struct108 is a non-union 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). 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 array of such types). A POD class is a class that is either a POD struct or a POD union.

As far as I can tell the pre C++11 standard does not diff much in the above items.

Shafik Yaghmour
  • 154,301
  • 39
  • 440
  • 740