Reputation: 4386
This is about std::is_pod
, which detects whether a template is a plain old data type or not.
See the following code:
struct A {
public:
int m1;
int m2;
};
struct B {
public:
int m1;
private:
int m2;
};
struct C {
private:
int m1;
int m2;
};
int main()
{
std::cout << std::boolalpha;
std::cout << std::is_pod<A>::value << '\n'; // true
std::cout << std::is_pod<B>::value << '\n'; // false
std::cout << std::is_pod<C>::value << '\n'; // true
}
The 3 structs all look like POD to me. But apparently struct B
is not.
I don't understand why. To me, they all have a trivial constructor, move and copy operator. Destructor is certainly trivial too.
I blame it on using 2 access specifiers, but I can't find information about this.
Upvotes: 26
Views: 894
Reputation: 32576
According to the standard (9 Classes [class], emphasis mine):
A standard-layout class is a class that:
...
— has the same access control (Clause 11) for all non-static data members,
...
and
A POD struct is a non-union class that is both a trivial class and a standard-layout class, and ...
Your hunch is correct, because B.m1
and B.m2
are both non-static and have different access control.
Upvotes: 31