titanium
titanium

Reputation: 55

Can a non-aggregate class be a POD class C++

From what I know if a class is not an aggregate then it is sure not a POD.

However in the following code

#include <iostream>
#include <type_traits>

class NotAggregate2
{
  int x; //x is private by default and non-static 
};

int main()
{
    std::cout << std::boolalpha;
    std::cout << std::is_pod<NotAggregate2>::value << '\n';
    std::cout << std::is_aggregate <NotAggregate2>::value << '\n';
}

which have output:

true
false

NotAggregate2 is not aggregate but a POD type.

So can a non-aggregate class be a POD class?

Upvotes: 2

Views: 115

Answers (1)

Brian61354270
Brian61354270

Reputation: 14434

Prior to C++11, you'd be correct: POD classes needed to be aggregate types, which in turn could not have private non-static data members. Post-C++11 however, POD types which are classes no longer need to be aggregates. Rather, POD types just need to satisfy the looser requirements of:

  1. Being a standard layout type, which only requires that all non-static data members have the same access control, not necessarily public
  2. Being a trivial type
  3. For class types, having no non-POD non-static data members (or arrays thereof)

In C++20, std::is_pod and the POD concept have been deprecated in favor of finer-grain properties and their tests, such as std::is_standard_layout, std::is_trivial, std::is_trivially_copyable, and std::is_aggregate.

Upvotes: 3

Related Questions