简体   繁体   English

一个包含所有私有成员的类可以是一个 POD 类吗?

[英]Can a class with all private members be a POD class?

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):我之前听说 POD 类型不能有私有数据——但根据 C++0x 草案,我的要求更宽松(强调我的):

has the same access control (Clause 11) for all non-static data members对所有非静态数据成员具有相同的访问控制(第 11 条)

which seems to suggest that private data is okay so long as it's all private.这似乎表明私人数据是可以的,只要它都是私人的。 I don't have a copy of C++03 though to check...我没有 C++03 的副本来检查...

Would then, WindowsApi::Uuid be a POD class?那么, WindowsApi::Uuid会是一个 POD 类吗?

namespace WindowsApi
{
    class Uuid
    {
        union
        {
            ::UUID asUuid; //Win32's UUID struct
            unsigned __int64 asInt64s[2];
            unsigned __int32 asInt32s[4];
        };
    public:
        Uuid() {}
        Uuid(::UUID sourceStructure) : asUuid(sourceStructure) {}
        operator ::UUID() { return asUuid; }
    };
}

I've heard before that POD types cannot have private data我之前听说 POD 类型不能有私有数据

In C++03 POD types cannot have private data (see AndreyT's answer).在 C++03 中,POD 类型不能有私有数据(参见 AndreyT 的回答)。

However the definition of POD has been changed in C++0x (See 9/10 ).然而 POD 的定义在 C++0x 中已经改变(见9/10 )。

As per n3225根据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). POD 结构是一个既是普通类又是标准布局类的类,并且没有非 POD 结构、非 POD 联合(或此类类型的数组)类型的非静态数据成员。
... ... ……

A POD class is a class that is either a POD struct or a POD union. POD 类是一个既可以是 POD 结构体也可以是 POD 联合体的类。

That means这意味着

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

is POD in C++0x because demo is both trivial and standard layout .是 C++0x 中的 POD,因为demo既简单又标准布局

The definition of Standard layout is in section 9/7标准布局的定义在第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),没有虚函数(10.3)和虚基类(10.1),
  • has the same access control (Clause 11) for all non-static data members ,对所有非静态数据成员具有相同的访问控制(第 11 条)
  • 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没有与第一个非静态数据成员相同类型的基类。 11

. .

Would then, WindowsApi::Uuid be a POD class?那么,WindowsApi::Uuid 会是一个 POD 类吗?

Nopes!不! WindowsApi::Uuid is neither POD in C++03 nor in C++0x. WindowsApi::Uuid在 C++03 和 C++0x 中都不是 POD。 A trivial class is a class that has a trivial default constructor (12.1) and is trivially copyable.平凡类是具有平凡默认构造函数(12.1) 且可简单复制的类。 WindowsApi::Uuid has a non trivial default constructor. WindowsApi::Uuid有一个重要的默认构造函数。

So this rule got relaxed in C++0x then?那么这个规则在 C++0x 中放松了吗?

Yes!是的! (Considering Clause 11) (考虑到第 11 条)

Also check out the FAQ entry on Aggregates and PODs另请查看有关聚合和 POD常见问题解答条目

C++03 still does not allow non-static private or protected data in POD classes. C++03 仍然不允许在 POD 类中使用非静态私有或受保护的数据。 This requirement is specified in the definition of aggregate该要求在聚集体的定义中指定

An aggregate is an array or a class (clause 9) with no user-declared constructors (12.1), no private or protected non-static data members (clause 11) , no base classes (clause 10), and no virtual functions (10.3).聚合是一个数组或类(第 9 条),没有用户声明的构造函数(12.1),没有私有或受保护的非静态数据成员(第 11 条) ,没有基类(第 10 条),也没有虚函数(10.3) )。

and POD class must be an aggregate first.并且 POD 类必须首先是一个聚合。

According to my n3225 C++0x draft, WindowsApi::Uuid is a POD class.根据我的 n3225 C++0x 草案, WindowsApi::Uuid是一个 POD 类。

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).从第 219 页:POD 结构是一个既是普通类又是标准布局类的类,并且没有非 POD 结构、非 POD 联合(或此类类型的数组)类型的非静态数据成员。

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),没有非平凡的复制构造函数(12.8),
  • has no non-trivial move constructors (12.8),没有非平凡的移动构造函数(12.8),
  • has no non-trivial copy assignment operators (13.5.3, 12.8),没有非平凡的复制赋值运算符 (13.5.3, 12.8),
  • has no non-trivial move assignment operators (13.5.3, 12.8), and没有重要的移动赋值运算符 (13.5.3, 12.8),和
  • has a trivial destructor (12.4).有一个简单的析构函数 (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),没有虚函数(10.3)和虚基类(10.1),
  • has the same access control (Clause 11) for all non-static data members ,对所有非静态数据成员具有相同的访问控制(第 11 条)
  • 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.由于WindowsApi不违反任何这些约束,因此它将是 C++0x 下的有效 POD 类。 As AndreyT mentions, this is a more generous wording than C++03.正如 AndreyT 所提到的,这是一个比 C++03 更慷慨的措辞。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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