我遇到过几次POD-type这个术语。 这是什么意思?
当前回答
非常非正式地:
POD是一种类型(包括类),c++编译器保证结构中不会发生“魔法”:例如,指向虚表的隐藏指针,转换为其他类型时应用于地址的偏移量(至少如果目标也是POD),构造函数或析构函数。粗略地说,当类型中只有内置类型和它们的组合时,类型就是POD。结果是某种“像”C类型的东西。
非正式地:
int, char, wchar_t, bool, float, double are PODs, as are long/short and signed/unsigned versions of them. pointers (including pointer-to-function and pointer-to-member) are PODs, enums are PODs a const or volatile POD is a POD. a class, struct or union of PODs is a POD provided that all non-static data members are public, 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? Wikipedia is wrong to say that a POD cannot have members of type pointer-to-member. Or rather, it's correct for the C++98 wording, but TC1 made explicit that pointers-to-member are POD.
形式上(c++ 03标准):
3.9(10): "Arithmetic types (3.9.1), enumeration types, pointer types, and pointer to member types (3.9.2) and cv-qualified versions of these types (3.9.3) are collectively caller scalar types. Scalar types, POD-struct types, POD-union types (clause 9), arrays of such types and cv-qualified versions of these types (3.9.3) are collectively called POD types" 9(4): "A POD-struct is an aggregate class that has no non-static data members of type non-POD-struct, non-POD-union (or array of such types) or reference, and has no user-define copy operator and no user-defined destructor. Similarly a POD-union is an aggregate union that has no non-static data members of type non-POD-struct, non-POD-union (or array of such types) or reference, and has no user-define copy operator and no user-defined destructor. 8.5.1(1): "An aggregate is an array or 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)."
其他回答
POD的概念和类型特征std::is_pod将在c++ 20中被弃用。有关进一步信息,请参阅此问题。
POD代表普通旧数据——也就是说,一个没有构造函数、析构函数和虚成员函数的类(无论是用关键字struct还是用关键字class定义的)。维基百科关于POD的文章更详细一些,并将其定义为:
c++中的普通旧数据结构是一个聚合类,它只包含PODS作为成员,没有用户定义的析构函数,没有用户定义的复制赋值操作符,也没有指针到成员类型的非静态成员。
更多的细节可以在c++ 98/03的答案中找到。c++ 11改变了围绕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开放了很多东西,允许用户重新引入内置构造函数。
非常非正式地:
POD是一种类型(包括类),c++编译器保证结构中不会发生“魔法”:例如,指向虚表的隐藏指针,转换为其他类型时应用于地址的偏移量(至少如果目标也是POD),构造函数或析构函数。粗略地说,当类型中只有内置类型和它们的组合时,类型就是POD。结果是某种“像”C类型的东西。
非正式地:
int, char, wchar_t, bool, float, double are PODs, as are long/short and signed/unsigned versions of them. pointers (including pointer-to-function and pointer-to-member) are PODs, enums are PODs a const or volatile POD is a POD. a class, struct or union of PODs is a POD provided that all non-static data members are public, 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? Wikipedia is wrong to say that a POD cannot have members of type pointer-to-member. Or rather, it's correct for the C++98 wording, but TC1 made explicit that pointers-to-member are POD.
形式上(c++ 03标准):
3.9(10): "Arithmetic types (3.9.1), enumeration types, pointer types, and pointer to member types (3.9.2) and cv-qualified versions of these types (3.9.3) are collectively caller scalar types. Scalar types, POD-struct types, POD-union types (clause 9), arrays of such types and cv-qualified versions of these types (3.9.3) are collectively called POD types" 9(4): "A POD-struct is an aggregate class that has no non-static data members of type non-POD-struct, non-POD-union (or array of such types) or reference, and has no user-define copy operator and no user-defined destructor. Similarly a POD-union is an aggregate union that has no non-static data members of type non-POD-struct, non-POD-union (or array of such types) or reference, and has no user-define copy operator and no user-defined destructor. 8.5.1(1): "An aggregate is an array or 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)."
据我所知,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)
参考:
http://en.cppreference.com/w/cpp/types/is_pod http://en.wikipedia.org/wiki/Plain_old_data_structure http://en.wikipedia.org/wiki/Plain_Old_C++_Object 文件type_traits
推荐文章
- 为什么这个结合赋值和相等检查的if语句返回true?
- cplusplus.com给出的错误、误解或坏建议是什么?
- 找出质数最快的算法是什么?
- time_t最终的类型定义是什么?
- c++枚举类可以有方法吗?
- 使arrayList.toArray()返回更具体的类型
- 格式化IO函数(*printf / *scanf)中的转换说明符%i和%d之间的区别是什么?
- 将析构函数设为私有有什么用?
- main()中的Return语句vs exit()
- 为什么c#不提供c++风格的'friend'关键字?
- 在函数的签名中添加关键字
- 我如何在Visual Studio中预处理后看到C/ c++源文件?
- 为什么在标准容器中使用std::auto_ptr<>是错误的?
- 用比较double和0
- 保护可执行文件不受逆向工程的影响?