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

前端 未结 3 1971
执念已碎
执念已碎 2021-01-18 03:41

I\'ve heard before that POD types cannot have private data -- but according to the C++0x draft I have the requirement is looser (emphasis mine):

has t

3条回答
  •  难免孤独
    2021-01-18 04:20

    I've heard before that POD types cannot have private data

    In C++03 POD types cannot have private data (see AndreyT's answer).

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

    As per n3225

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

    A POD class is a class that is either a POD struct or a POD union.

    That means

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

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

    The definition of Standard layout is in section 9/7

    A standard-layout class is a class that:

    • has no non-static data members of type non-standard-layout class (or array of such types) or reference,
    • has no virtual functions (10.3) and no virtual base classes (10.1),
    • has the same access control (Clause 11) for all non-static data members,
    • has no non-standard-layout base classes,
    • either has no non-static data members in the most-derived class and at most one base class with non-static data members, or has no base classes with non-static data members, and
    • has no base classes of the same type as the first non-static data member.11

    .

    Would then, WindowsApi::Uuid be a POD class?

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

    So this rule got relaxed in C++0x then?

    Yes! (Considering Clause 11)

    Also check out the FAQ entry on Aggregates and PODs

提交回复
热议问题