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

前端 未结 3 1980
执念已碎
执念已碎 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:11

    According to my n3225 C++0x draft, WindowsApi::Uuid is a POD class.

    From page 219: 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 trivial class is a class that has a trivial default constructor and is trivially copyable:

    A trivially copyable class is a class that:

    • has no non-trivial copy constructors (12.8),
    • has no non-trivial move constructors (12.8),
    • has no non-trivial copy assignment operators (13.5.3, 12.8),
    • has no non-trivial move assignment operators (13.5.3, 12.8), and
    • has a trivial destructor (12.4).

    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.

    Since WindowsApi doesn't violate any of these constraints, it will be a valid POD class under C++0x. As AndreyT mentions, this is a more generous wording than C++03.

提交回复
热议问题