我曾多次遇到过 POD 类型这个术语。
这是什么意思?
9 回答
POD代表Plain Old Data - 即没有构造函数、析构函数和虚拟成员函数的类(无论是用关键字struct
还是关键字定义)。Wikipedia 关于 POD 的文章更详细,并将其定义为:class
C++ 中的普通旧数据结构是一个聚合类,它只包含 PODS 作为成员,没有用户定义的析构函数,没有用户定义的复制赋值运算符,也没有指向成员类型的非静态成员。
可以在C++98/03 的这个答案中找到更详细的信息。C++11 改变了围绕 POD 的规则,大大放宽了它们,因此需要在此处进行后续回答。
非常不正式:
POD 是一种类型(包括类),其中 C++ 编译器保证结构中不会发生“魔法”:例如,指向 vtable 的隐藏指针、当地址转换为其他类型时应用于地址的偏移量(至少如果目标的 POD 也是如此)、构造函数或析构函数。粗略地说,当类型中唯一的东西是内置类型和它们的组合时,类型就是 POD。结果是“表现得像”C 类型的东西。
不那么正式:
int
,char
,wchar_t
,bool
,float
,double
是 POD,以及它们long/short
的signed/unsigned
版本。- 指针(包括指向函数的指针和指向成员的指针)是 POD,
enums
是 POD- a
const
或volatile
POD 是一个 POD。 - a
class
,struct
orunion
of POD 是一个 POD,前提是所有非静态数据成员都是public
,并且它没有基类,也没有构造函数、析构函数或虚拟方法。在此规则下,静态成员不会阻止某物成为 POD。此规则在 C++11 中已更改,并且允许某些私有成员:具有所有私有成员的类可以是 POD 类吗? - Wikipedia 说 POD 不能具有指向成员类型的成员是错误的。或者更确切地说,它对于 C++98 的措辞是正确的,但是 TC1 明确指出指向成员的指针是 POD。
正式(C++03 标准):
3.9(10): “算术类型 (3.9.1)、枚举类型、指针类型和指向成员类型的指针 (3.9.2) 和这些类型的 cv 限定版本 (3.9.3) 统称为调用者标量类型。标量类型、POD 结构类型、POD 联合类型(第 9 条)、此类类型的数组以及这些类型的 cv 限定版本(3.9.3)统称为 POD 类型”
9(4): “POD 结构是一个聚合类,它没有非 POD 结构、非 POD 联合(或此类类型的数组)或引用类型的非静态数据成员,并且没有用户-定义复制运算符并且没有用户定义的析构函数。类似地,POD 联合是一个聚合联合,它没有非 POD 结构、非 POD 联合(或此类类型的数组)或引用类型的非静态数据成员,并且没有用户定义的复制操作符和用户定义的析构函数。
8.5.1(1): “聚合是一个数组或类(第 9 条),没有用户声明的构造函数(12.1),没有私有或受保护的非静态数据成员(第 11 条),没有基类(第 10 条)并且没有虚拟功能(10.3)。”
简而言之,它是所有内置数据类型(例如int
, char
, float
, long
, unsigned char
, double
, 等)和 POD 数据的所有聚合。是的,这是一个递归定义。;)
更清楚地说,POD 就是我们所说的“结构”:一个单元或一组单元,只存储数据。
为什么我们需要区分 POD 和非 POD?
C++ 作为 C 的扩展开始了它的生命。虽然现代 C++ 不再是 C 的严格超集,但人们仍然期望两者之间具有高度的兼容性。平台的“C ABI”也经常充当平台上其他语言的事实上的标准跨语言 ABI。
粗略地说,POD 类型是与 C 兼容的类型,也许同样重要的是与某些 ABI 优化兼容。
为了与 C 兼容,我们需要满足两个约束。
- 布局必须与对应的 C 类型相同。
- 类型必须以与对应的 C 类型相同的方式传递给函数和从函数返回。
某些 C++ 功能与此不兼容。
虚方法要求编译器插入一个或多个指向虚方法表的指针,这在 C 中是不存在的。
用户定义的复制构造函数、移动构造函数、复制赋值和析构函数对参数传递和返回都有影响。许多 C ABI 在寄存器中传递和返回小参数,但传递给用户定义的构造函数/赋值/析构函数的引用只能与内存位置一起使用。
因此,需要定义哪些类型可以预期是“C 兼容的”,哪些类型不能。C++03 在这方面有点过于严格,任何用户定义的构造函数都会禁用内置的构造函数,并且任何将它们添加回来的尝试都会导致它们是用户定义的,因此类型是非 pod。通过允许用户重新引入内置构造函数,C++11 打开了很多东西。
static_assert
具有从 C++11 到 C++17 和 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
#include <type_traits>
#include <array>
#include <vector>
int main() {
#if __cplusplus >= 201103L
// # Not POD
//
// Non-POD examples. Let's just walk all non-recursive non-POD branches of cppreference.
{
// Non-trivial implies non-POD.
// https://en.cppreference.com/w/cpp/named_req/TrivialType
{
// Has one or more default constructors, all of which are either
// trivial or deleted, and at least one of which is not deleted.
{
// Not trivial because we removed the default constructor
// by using our own custom non-default constructor.
{
struct C {
C(int) {}
};
static_assert(std::is_trivially_copyable<C>(), "");
static_assert(!std::is_trivial<C>(), "");
static_assert(!std::is_pod<C>(), "");
}
// No, this is not a default trivial constructor either:
// https://en.cppreference.com/w/cpp/language/default_constructor
//
// The constructor is not user-provided (i.e., is implicitly-defined or
// defaulted on its first declaration)
{
struct C {
C() {}
};
static_assert(std::is_trivially_copyable<C>(), "");
static_assert(!std::is_trivial<C>(), "");
static_assert(!std::is_pod<C>(), "");
}
}
// Not trivial because not trivially copyable.
{
struct C {
C(C&) {}
};
static_assert(!std::is_trivially_copyable<C>(), "");
static_assert(!std::is_trivial<C>(), "");
static_assert(!std::is_pod<C>(), "");
}
}
// Non-standard layout implies non-POD.
// https://en.cppreference.com/w/cpp/named_req/StandardLayoutType
{
// Non static members with different access control.
{
// i is public and j is private.
{
struct C {
public:
int i;
private:
int j;
};
static_assert(!std::is_standard_layout<C>(), "");
static_assert(!std::is_pod<C>(), "");
}
// These have the same access control.
{
struct C {
private:
int i;
int j;
};
static_assert(std::is_standard_layout<C>(), "");
static_assert(std::is_pod<C>(), "");
struct D {
public:
int i;
int j;
};
static_assert(std::is_standard_layout<D>(), "");
static_assert(std::is_pod<D>(), "");
}
}
// Virtual function.
{
struct C {
virtual void f() = 0;
};
static_assert(!std::is_standard_layout<C>(), "");
static_assert(!std::is_pod<C>(), "");
}
// Non-static member that is reference.
{
struct C {
int &i;
};
static_assert(!std::is_standard_layout<C>(), "");
static_assert(!std::is_pod<C>(), "");
}
// Neither:
//
// - has no base classes with non-static data members, or
// - has no non-static data members in the most derived class
// and at most one base class with non-static data members
{
// Non POD because has two base classes with non-static data members.
{
struct Base1 {
int i;
};
struct Base2 {
int j;
};
struct C : Base1, Base2 {};
static_assert(!std::is_standard_layout<C>(), "");
static_assert(!std::is_pod<C>(), "");
}
// POD: has just one base class with non-static member.
{
struct Base1 {
int i;
};
struct C : Base1 {};
static_assert(std::is_standard_layout<C>(), "");
static_assert(std::is_pod<C>(), "");
}
// Just one base class with non-static member: Base1, Base2 has none.
{
struct Base1 {
int i;
};
struct Base2 {};
struct C : Base1, Base2 {};
static_assert(std::is_standard_layout<C>(), "");
static_assert(std::is_pod<C>(), "");
}
}
// Base classes of the same type as the first non-static data member.
// TODO failing on GCC 8.1 -std=c++11, 14 and 17.
{
struct C {};
struct D : C {
C c;
};
//static_assert(!std::is_standard_layout<C>(), "");
//static_assert(!std::is_pod<C>(), "");
};
// C++14 standard layout new rules, yay!
{
// Has two (possibly indirect) base class subobjects of the same type.
// Here C has two base classes which are indirectly "Base".
//
// TODO failing on GCC 8.1 -std=c++11, 14 and 17.
// even though the example was copy pasted from cppreference.
{
struct Q {};
struct S : Q { };
struct T : Q { };
struct U : S, T { }; // not a standard-layout class: two base class subobjects of type Q
//static_assert(!std::is_standard_layout<U>(), "");
//static_assert(!std::is_pod<U>(), "");
}
// Has all non-static data members and bit-fields declared in the same class
// (either all in the derived or all in some base).
{
struct Base { int i; };
struct Middle : Base {};
struct C : Middle { int j; };
static_assert(!std::is_standard_layout<C>(), "");
static_assert(!std::is_pod<C>(), "");
}
// None of the base class subobjects has the same type as
// for non-union types, as the first non-static data member
//
// TODO: similar to the C++11 for which we could not make a proper example,
// but with recursivity added.
// TODO come up with an example that is POD in C++14 but not in C++11.
}
}
}
// # POD
//
// POD examples. Everything that does not fall neatly in the non-POD examples.
{
// Can't get more POD than this.
{
struct C {};
static_assert(std::is_pod<C>(), "");
static_assert(std::is_pod<int>(), "");
}
// Array of POD is POD.
{
struct C {};
static_assert(std::is_pod<C>(), "");
static_assert(std::is_pod<C[]>(), "");
}
// Private member: became POD in C++11
// https://stackoverflow.com/questions/4762788/can-a-class-with-all-private-members-be-a-pod-class/4762944#4762944
{
struct C {
private:
int i;
};
#if __cplusplus >= 201103L
static_assert(std::is_pod<C>(), "");
#else
static_assert(!std::is_pod<C>(), "");
#endif
}
// Most standard library containers are not POD because they are not trivial,
// which can be seen directly from their interface definition in the standard.
// https://stackoverflow.com/questions/27165436/pod-implications-for-a-struct-which-holds-an-standard-library-container
{
static_assert(!std::is_pod<std::vector<int>>(), "");
static_assert(!std::is_trivially_copyable<std::vector<int>>(), "");
// Some might be though:
// https://stackoverflow.com/questions/3674247/is-stdarrayt-s-guaranteed-to-be-pod-if-t-is-pod
static_assert(std::is_pod<std::array<int, 1>>(), "");
}
}
// # POD effects
//
// Now let's verify what effects does PODness have.
//
// Note that this is not easy to do automatically, since many of the
// failures are undefined behaviour.
//
// A good initial list can be found at:
// https://stackoverflow.com/questions/4178175/what-are-aggregates-and-pods-and-how-why-are-they-special/4178176#4178176
{
struct Pod {
uint32_t i;
uint64_t j;
};
static_assert(std::is_pod<Pod>(), "");
struct NotPod {
NotPod(uint32_t i, uint64_t j) : i(i), j(j) {}
uint32_t i;
uint64_t j;
};
static_assert(!std::is_pod<NotPod>(), "");
// __attribute__((packed)) only works for POD, and is ignored for non-POD, and emits a warning
// https://stackoverflow.com/questions/35152877/ignoring-packed-attribute-because-of-unpacked-non-pod-field/52986680#52986680
{
struct C {
int i;
};
struct D : C {
int j;
};
struct E {
D d;
} /*__attribute__((packed))*/;
static_assert(std::is_pod<C>(), "");
static_assert(!std::is_pod<D>(), "");
static_assert(!std::is_pod<E>(), "");
}
}
#endif
}
经测试:
for std in 11 14 17; do echo $std; g++-8 -Wall -Werror -Wextra -pedantic -std=c++$std pod.cpp; done
在 Ubuntu 18.04、GCC 8.2.0 上。
据我了解 POD (PlainOldData) 只是一个原始数据 - 它不需要:
- 建造,
- 被摧毁,
- 有自定义运算符。
- 不能有虚函数,
- 并且不得覆盖运算符。
如何检查某物是否是 POD?好吧,有一个结构叫做std::is_pod
:
namespace std {
// Could use is_standard_layout && is_trivial instead of the builtin.
template<typename _Tp>
struct is_pod
: public integral_constant<bool, __is_pod(_Tp)>
{ };
}
(来自标题 type_traits)
参考:
POD(普通旧数据)对象具有这些数据类型之一——基本类型、指针、联合、结构、数组或类——没有构造函数。相反,非 POD 对象是存在构造函数的对象。POD 对象的生命周期在它获得具有适合其类型的适当大小的存储时开始,它的生命周期在对象的存储被重用或释放时结束。
PlainOldData 类型也不能有任何:
- 虚函数(它们自己的或继承的)
- 虚拟基类(直接或间接)。
PlainOldData 更宽松的定义包括带有构造函数的对象;但不包括那些拥有虚拟任何东西的人。PlainOldData 类型的重要问题是它们是非多态的。继承可以用 POD 类型完成,但是它应该只用于 ImplementationInheritance(代码重用)而不是多态/子类型。
一个常见的(虽然不是严格正确的)定义是 PlainOldData 类型是没有 VeeTable 的任何东西。
POD 的概念和类型特征std::is_pod
将在 C++20 中弃用。有关更多信息,请参阅此问题。
对于 C++,Plain Old Data 不仅仅意味着 int、char 等是唯一使用的类型。普通旧数据实际上意味着您可以将 struct memcpy 从内存中的一个位置带到另一个位置,并且事情将完全按照您的预期工作(即不会爆炸)。如果您的类或您的类包含的任何类的成员是指针或引用或具有虚函数的类,则会中断。本质上,如果必须在某处涉及指针,则它不是普通的旧数据。