C++ 中的 POD 类型是什么?
我曾多次遇到过 POD 类型这个术语。
这是什么意思?
I've come across this term POD-type a few times.
What does it mean?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
我曾多次遇到过 POD 类型这个术语。
这是什么意思?
I've come across this term POD-type a few times.
What does it mean?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(9)
从 C++11 到 C++17 具有
static_assert
的所有非 POD 案例以及 POD 效果的示例已添加
std::is_pod
在 C++11 中,所以现在让我们考虑该标准。std::is_pod
已被弃用 C++20,如 https://stackoverflow.com/a 中所述/48435532/895245,让我们在更换的支持到来时更新此内容。随着标准的发展,POD 限制变得越来越宽松,我的目标是通过 ifdefs 涵盖示例中的所有放松。
libstdc++ 在以下位置进行了少量测试: https://github.com/gcc-mirror/gcc/blob/gcc-8_2_0-release/libstdc%2B%2B-v3/testsuite/20_util/is_pod/value.cc 但它太少了。 维护者:如果您阅读本文,请合并此内容。 我懒得查看以下提到的所有 C++ 测试套件项目: https://softwareengineering.stackexchange.com/questions/199708/is-there-a-compliance-test-for-c-compilers
GitHub 上游。
测试环境:
Ubuntu 18.04、GCC 8.2.0。
Examples of all non-POD cases with
static_assert
from C++11 to C++17 and POD effectsstd::is_pod
has been added in C++11, so let's consider that standard onwards for now.std::is_pod
has been deprecated C++20 as mentioned at https://stackoverflow.com/a/48435532/895245 , let's update this as support arrives for the replacements.POD restrictions have become more and more relaxed as the standard evolved, I aim to cover all relaxations in the example through ifdefs.
libstdc++ has at tiny bit of testing at: https://github.com/gcc-mirror/gcc/blob/gcc-8_2_0-release/libstdc%2B%2B-v3/testsuite/20_util/is_pod/value.cc but it just too little. Maintainers: please merge this if you read this post. I'm lazy to check out all the C++ testsuite projects mentioned at: https://softwareengineering.stackexchange.com/questions/199708/is-there-a-compliance-test-for-c-compilers
GitHub upstream.
Tested with:
on Ubuntu 18.04, GCC 8.2.0.
POD 代表 Plain Old Data - 即一个类(无论是使用关键字
struct
定义还是使用关键字class
) 没有构造函数、析构函数和虚拟成员函数。 Wikipedia 关于 POD 的文章 更详细,并将其定义为:更多详细信息可以在C++98/03 的答案中找到。 C++11 改变了 POD 周围的规则,大大放宽了它们,因此需要在此处提供后续答案。
POD stands for Plain Old Data - that is, a class (whether defined with the keyword
struct
or the keywordclass
) without constructors, destructors and virtual members functions. Wikipedia's article on POD goes into a bit more detail and defines it as:Greater detail can be found in this answer for C++98/03. C++11 changed the rules surrounding POD, relaxing them greatly, thus necessitating a follow-up answer here.
非常通俗地说:
POD 是一种类型(包括类),其中 C++ 编译器保证结构中不会发生任何“魔法”:例如,指向 vtable 的隐藏指针、在转换为地址时应用于地址的偏移量其他类型(至少目标的 POD 也是如此)、构造函数或析构函数。 粗略地说,当一个类型中只有内置类型及其组合时,该类型就是 POD。 结果是“表现得像”C 类型。
不太非正式的:
int
、char
、wchar_t
、bool
、float
、double
是 POD,它们的长/短
和有符号/无符号
版本也是如此。enum
是 POD,const
或易失性
POD 是 POD 。class
、struct
或union
是一个 POD,前提是所有非静态数据成员都是public
,它没有基类,也没有构造函数、析构函数或虚方法。 在此规则下,静态成员不会阻止某些内容成为 POD。 此规则在 C++11 中已更改,并且允许某些私有成员: 一个包含所有私有成员的类可以是 POD 类吗?形式上(C++03 标准):
Very informally:
A POD is a type (including classes) where the C++ compiler guarantees that there will be no "magic" going on in the structure: for example hidden pointers to vtables, offsets that get applied to the address when it is cast to other types (at least if the target's POD too), constructors, or destructors. Roughly speaking, a type is a POD when the only things in it are built-in types and combinations of them. The result is something that "acts like" a C type.
Less informally:
int
,char
,wchar_t
,bool
,float
,double
are PODs, as arelong/short
andsigned/unsigned
versions of them.enums
are PODsconst
orvolatile
POD is a POD.class
,struct
orunion
of PODs is a POD provided that all non-static data members arepublic
, and it has no base class and no constructors, destructors, or virtual methods. Static members don't stop something being a POD under this rule. This rule has changed in C++11 and certain private members are allowed: Can a class with all private members be a POD class?Formally (C++03 Standard):
C++ 最初是作为 C 的扩展而诞生的。虽然现代 C++ 不再是 C 的严格超集,但人们仍然期望两者之间具有高水平的兼容性。 平台的“C ABI”也经常充当该平台上其他语言的事实标准间语言 ABI。
粗略地说,POD 类型是与 C 兼容的类型,也许同样重要的是与某些 ABI 优化兼容。
为了与C兼容,我们需要满足两个约束。
某些 C++ 功能与此不兼容。
虚拟方法要求编译器插入一个或多个指向虚拟方法表的指针,这在 C 中不存在。
用户定义的复制构造函数、移动构造函数、复制赋值和析构函数对参数传递和返回有影响。 许多 C ABI 在寄存器中传递和返回小参数,但传递给用户定义的构造函数/赋值函数/析构函数的引用只能使用内存位置。
因此需要定义哪些类型可以“C 兼容”,哪些类型不能。 C++03 在这方面有点过于严格,任何用户定义的构造函数都会禁用内置构造函数,并且任何将它们添加回来的尝试都会导致它们是用户定义的,因此类型是非 Pod 的。 C++11 通过允许用户重新引入内置构造函数,使事情变得更加开放。
C++ started its life as an extension of C. While modern C++ is no longer a strict superset of C, people still expect a high level of compatibility between the two. The "C ABI" of a platform also frequently acts as a de-facto standard inter-language ABI for other languages on the platform.
Roughly speaking, a POD type is a type that is compatible with C and perhaps equally importantly is compatible with certain ABI optimisations.
To be compatible with C, we need to satisfy two constraints.
Certain C++ features are incompatible with this.
Virtual methods require the compiler to insert one or more pointers to virtual method tables, something that doesn't exist in C.
User-defined copy constructors, move constructors, copy assignments and destructors have implications for parameter passing and returning. Many C ABIs pass and return small parameters in registers, but the references passed to the user defined constructor/assigment/destructor can only work with memory locations.
So there is a need to define what types can be expected to be "C compatible" and what types cannot. C++03 was somewhat over-strict in this regard, any user-defined constructor would disable the built-in constructors and any attempt to add them back in would result in them being user-defined and hence the type being non-pod. C++11 opened things up quite a bit, by allowing the user to re-introduce the built-in constructors.
Plain Old Data
简而言之,都是内置数据类型(例如
int< /code>、
char
、浮点
、long
、无符号字符
、双精度
、等)以及 POD 数据的所有聚合。 是的,这是一个递归定义。 ;)更清楚地说,POD 就是我们所说的“结构”:一个或一组仅存储数据的单元。
Plain Old Data
In short, it is all built-in data types (e.g.
int
,char
,float
,long
,unsigned char
,double
, etc.) and all aggregation of POD data. Yes, it's a recursive definition. ;)To be more clear, a POD is what we call "a struct": a unit or a group of units that just store data.
据我了解,POD(PlainOldData)只是原始数据 - 它不需要:
如何检查某物是否是 POD? 好吧,有一个名为
std::is_pod
的结构:(来自标头 type_traits)
参考:
As I understand POD (PlainOldData) is just a raw data - it does not need:
How to check if something is a POD? Well, there is a struct for that called
std::is_pod
:(From header type_traits)
Reference:
POD 的概念和类型特征
std::is_pod
在 C++20 中已被弃用。 有关更多信息,请参阅为什么在 C++20 中不推荐使用 std::is_pod?。The concept of POD and the type trait
std::is_pod
has been deprecated in C++20. See Why is std::is_pod deprecated in C++20? for further information.POD(普通旧数据)对象具有以下数据类型之一——基本类型、指针、联合、结构、数组或类——没有构造函数。 相反,非 POD 对象是存在构造函数的对象。 当 POD 对象获得适合其类型的适当大小的存储时,它的生命周期开始;当对象的存储被重用或释放时,它的生命周期结束。
PlainOldData 类型也不得具有以下任何功能:
PlainOldData 的宽松定义包括带有构造函数的对象; 但不包括那些拥有虚拟任何东西的人。 PlainOldData 类型的重要问题是它们是非多态的。 继承可以使用 POD 类型来完成,但是它只能用于实现继承(代码重用),而不是多态/子类型。
一个常见的(尽管不是严格正确的)定义是 PlainOldData 类型是任何没有 VeeTable 的类型。
A POD (plain old data) object has one of these data types--a fundamental type, pointer, union, struct, array, or class--with no constructor. Conversely, a non-POD object is one for which a constructor exists. A POD object begins its lifetime when it obtains storage with the proper size for its type and its lifetime ends when the storage for the object is either reused or deallocated.
PlainOldData types also must not have any of:
A looser definition of PlainOldData includes objects with constructors; but excludes those with virtual anything. The important issue with PlainOldData types is that they are non-polymorphic. Inheritance can be done with POD types, however it should only be done for ImplementationInheritance (code reuse) and not polymorphism/subtyping.
A common (though not strictly correct) definition is that a PlainOldData type is anything that doesn't have a VeeTable.
对于 C++,普通旧数据不仅仅意味着 int、char 等是唯一使用的类型。 普通旧数据实际上意味着您可以将 struct memcpy 从内存中的一个位置复制到另一个位置,并且事情将完全按照您的预期工作(即不会崩溃)。 如果您的类或您的类包含的任何类的成员是指针或引用或具有虚函数的类,则此情况会中断。 本质上,如果必须在某个地方涉及指针,那么它就不是普通的旧数据。
With C++, Plain Old Data doesn't just mean that things like int, char, etc are the only types used. Plain Old Data really means in practice that you can take a struct memcpy it from one location in memory to another and things will work exactly like you would expect (i.e. not blow up). This breaks if your class, or any class your class contains, has as a member that is a pointer or a reference or a class that has a virtual function. Essentially, if pointers have to be involved somewhere, its not Plain Old Data.