Plain old data structure, 缩写为POD, 是C++语言的标准中定义的一类数据结构,POD适用于需要明确的数据底层操作的系统中。POD通常被用在系统的边界处,即指不同系统之间只能以底层数据的形式进行交互,系统的高层逻辑不能互相兼容。比如当对象的字段值是从外部数据中构建时,系统还没有办法对对象进行语义检查和解释,这时就适用POD来存储数据。

定义

POD类型包括下述C++类型,以及其cv-qualified的类型,还有以其为基类型的数组类型:

  • 标量类型(scalar type)
  • POD类类型(POD class type)

标量类型

术语标量类型包括下述C++类型范畴, 以及其cv-qualified类型:

  • 算术类型(arithmetic type)
  • 枚举类型(enumeration type)
  • 指针类型(pointer type)
  • 指针到成员类型(pointer-to-member type)

术语算术类型包括下述C++类型范畴:

  • 整数类型(integral type)
  • 浮点类型(floating type)

术语整数类型包括下述C++类型范畴:

  • 有符号整数类型 (signed char, short, int, long),
  • 无符号整数类型(unsigned char, unsigned short, unsigned int, unsigned long),
  • 字符类型char与宽字符类型wchar_t
  • 布尔类型bool。
  • 术语浮点类型包括C++的float, double, and long double类型.

术语枚举类型包括各种枚举类型,即命名的常量值(named constant values)的集合.

术语指针类型包括下述C++类型范畴:

  • 空指针pointer-to-void (void *),
  • 对象指针pointer-to-object与指向静态数据成员的指针pointer-to-static-member-data (都是形如为T*,其中T是对象类型),
  • 函数指针pointer-to-function与指向静态成员函数的指针pointer-to-static-member-function (都是形如T (*)(...),T是函数的返回值的类型).

术语指针到成员类型包括下述C++类型范畴:

  • 指针到非静态数据成员(pointer-to-nonstatic-member-data), 形如T C::* 表示指向类C的类型为T的数据成员的指针;
  • 指针到非静态成员函数(pointer-to-nonstatic-member-functions), 形如T (C::*)(...) 表示指向类C的返回值类型为T的成员函数的指针.

POD类类型

POD类类型是指聚合类(aggregate classes, 即POD-struct types)与聚合union (POD-union types),且不具有下述成员:

  • 指针到成员类型的非静态数据成员(包括数组)。
  • 非POD类类型的非静态数据成员(包括数组)。
  • 引用类型的(reference type)非静态数据成员。
  • 用户定义的拷贝与赋值算子。
  • 用户定义的析构函数。

术语聚合是指任何的数组或者类,且不具有下述特征:

  • 用户定义的构造函数。
  • 私有或保护的非静态数据成员。
  • 基类。
  • 虚函数。

可见,POD类类型就是指class、struct、union,且不具有用户定义的构造函数、析构函数、拷贝算子、赋值算子;不具有继承关系,因此没有基类;不具有虚函数,所以就没有虚表;非静态数据成员没有私有或保护属性的、没有引用类型的、没有非POD类类型的(即嵌套类都必须是POD)、没有指针到成员类型的(因为这个类型内含了this指针)。

用途

POD类型在源代码兼容于ANSI C时非常重要。POD对象与C语言的对应对象具有共同的一些特性,包括初始化、复制、内存布局、寻址。

一个例子是下述C++的new表达式中的对象初始化,POD与non-POD的区别: non-POD类型的对象或数组总是被初始化;而POD类型的对象或数组可能未被初始化.

其它与POD相关的C++特性:

  • 内存布局——POD对象的组成字节是连续的.
  • 初始化——对于non-const POD对象,如果没有初始化声明时,具有不确定的初值(indeterminate initial value) . POD对象的缺省初始化为0值. 静态POD对象初始化为给定的初值,如果是局部静态POD对象,在进入所在作用域之前初始化; 对于非局部静态POD对象,在任何动态初始化之前赋予初值.
  • 拷贝——POD对象可直接拷贝(例如用memcpy())到其它字符数组或相同POD类型的对象,保持其值不变。POD类型可以用作标准模板字符串类的字符. 由于这个原因,函数的返回值如果是non-POD类型,则不能通过寄存器传递函数的返回值。
  • 寻址——一个POD对象的地址可以是一个地址常量表达式;一个对POD成员的引用可以是一个引用常量表达式. 一个POD-struct对象的指针,适合用reinterpret_cast转换到它的初始值.
 #include <iostream>
using namespace std; /// POD
template<typename T>
struct POD
{
static const bool Result=false;
};
template<>struct POD<bool>{static const bool Result=true;};
template<>struct POD<signed char>{static const bool Result=true;};
template<>struct POD<unsigned char>{static const bool Result=true;};
template<>struct POD<signed short>{static const bool Result=true;};
template<>struct POD<unsigned short>{static const bool Result=true;};
template<>struct POD<signed int>{static const bool Result=true;};
template<>struct POD<unsigned int>{static const bool Result=true;};
template<>struct POD<signed long long>{static const bool Result=true;};
template<>struct POD<unsigned long long>{static const bool Result=true;};
template<>struct POD<char>{static const bool Result=true;};
template<>struct POD<wchar_t>{static const bool Result=true;};
template<typename T>struct POD<T*>{static const bool Result=true;};
template<typename T>struct POD<T&>{static const bool Result=true;};
template<typename T, typename C>struct POD<T C::*>{static const bool Result=true;};
template<typename T, int _Size>struct POD<T[_Size]>{static const bool Result=POD<T>::Result;};
template<typename T>struct POD<const T>{static const bool Result=POD<T>::Result;};
template<typename T>struct POD<volatile T>{static const bool Result=POD<T>::Result;};
template<typename T>struct POD<const volatile T>{static const bool Result=POD<T>::Result;}; /// Test Def
class MyClass{int a,b;};
class MyPOD{int a,b;};
typedef int* PTI;
typedef int ARR[];
typedef int& BAS;
typedef const int CON;
typedef std::pair<int,int> PII;
typedef std::pair<MyClass,MyPOD> PMM;
template<>struct POD<MyPOD>{static const bool Result=true;};
template<typename K, typename V>struct POD< std::pair<K, V> >{static const bool Result=POD<K>::Result && POD<V>::Result;}; int main()
{
/// Test Code
cout << POD<int>::Result << endl;
cout << POD<PTI>::Result << endl;
cout << POD<ARR>::Result << endl;
cout << POD<BAS>::Result << endl;
cout << POD<CON>::Result << endl;
cout << POD<MyClass>::Result << endl;
cout << POD<MyPOD>::Result << endl;
cout << POD<PII>::Result << endl;
cout << POD<PMM>::Result << endl;
return ;
}

Plain old data structure(POD)的更多相关文章

  1. Plain Old Data (POD)

    Plain Old Data (POD) POD指的是这样一些数据类型:基本数据类型.指针.union.数组.构造函数是 trivial 的 struct 或者 class. POD用来表明C++中与 ...

  2. [LeetCode] All O`one Data Structure 全O(1)的数据结构

    Implement a data structure supporting the following operations: Inc(Key) - Inserts a new key with va ...

  3. [LeetCode] Add and Search Word - Data structure design 添加和查找单词-数据结构设计

    Design a data structure that supports the following two operations: void addWord(word) bool search(w ...

  4. [LeetCode] Two Sum III - Data structure design 两数之和之三 - 数据结构设计

    Design and implement a TwoSum class. It should support the following operations:add and find. add - ...

  5. Finger Trees: A Simple General-purpose Data Structure

    http://staff.city.ac.uk/~ross/papers/FingerTree.html Summary We present 2-3 finger trees, a function ...

  6. Mesh Data Structure in OpenCascade

    Mesh Data Structure in OpenCascade eryar@163.com 摘要Abstract:本文对网格数据结构作简要介绍,并结合使用OpenCascade中的数据结构,将网 ...

  7. ✡ leetcode 170. Two Sum III - Data structure design 设计two sum模式 --------- java

    Design and implement a TwoSum class. It should support the following operations: add and find. add - ...

  8. leetcode Add and Search Word - Data structure design

    我要在这里装个逼啦 class WordDictionary(object): def __init__(self): """ initialize your data ...

  9. Java for LeetCode 211 Add and Search Word - Data structure design

    Design a data structure that supports the following two operations: void addWord(word)bool search(wo ...

随机推荐

  1. 【BZOJ 3387】 线段树= =

    57 跨栏训练为了让奶牛参与运动,约翰建造了 K 个栅栏.每条栅栏可以看做是二维平面上的一条线段,它们都平行于 X 轴.第 i 条栅栏所覆盖的 X 轴坐标的区间为 [ Ai,Bi ], Y 轴高度就是 ...

  2. 【USACO 2012 Open】Running Laps(树状数组)

    53 奶牛赛跑 约翰有 N 头奶牛,他为这些奶牛准备了一个周长为 C 的环形跑牛场.所有奶牛从起点同时起跑,奶牛在比赛中总是以匀速前进的,第 i 头牛的速度为 Vi.只要有一头奶牛跑完 L 圈之后,比 ...

  3. Lua table pair和ipair区别

    官方描述: ipairs (t) Returns three values: an iterator function, the table t, and 0, so that the constru ...

  4. 【原创】如何构建MIPS交叉编译工具链

    运行环境:Ubuntu12.04PC提前安装库:flex,bison,libncureses5-dev,texinfo,这些库提前apt-get install.需要重新安装:gawk(先apt-ge ...

  5. WordPress Complete Gallery Manager插件‘upload-images.php’任意文件上传漏洞

    漏洞名称: WordPress Complete Gallery Manager插件‘upload-images.php’任意文件上传漏洞 CNNVD编号: CNNVD-201309-377 发布时间 ...

  6. ☀【CSS3】形状

    CSS3shapeshttp://www.css3shapes.com/ <!DOCTYPE html> <html lang="zh-CN"> <h ...

  7. Automator 简单使用流程

    iOS开发中常常要用到图片缩放的工作,有些需求流程很奇葩,根本找不到现成的工具去实现. 这时候,你可以去想一想Automator了. 示例:要把文件夹下所有的图片文件都缩小成原来的一半(搞iOS开发的 ...

  8. 【转】SourceTree的简单使用

    原文网址:http://blog.csdn.net/u011439289/article/details/42126507 今天开始参与公司项目的代码编写,公司内部采用的是gitlib,所以用到了So ...

  9. android开发板

    element14-beaglebone-black http://www.embest-tech.cn/shop/star/element14-beaglebone-black-rev-c.html ...

  10. Dot Net设计模式—MVC模式

    1 MVC设计模式简介 MVC结构是为那些需要为同样的数据提供多个视图的应用程序而设计的,它很好的实现了数据层与表示层的分离.MVC作为一种开发模型,通常用于分布式应用系统的设计和分析中,以及用于确定 ...