简体   繁体   中英

Why is this not a POD?

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM