定义

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 (POD) (转)的更多相关文章

  1. Plain Old Data (POD)

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

  2. Plain old data structure(POD)

    Plain old data structure, 缩写为POD, 是C++语言的标准中定义的一类数据结构,POD适用于需要明确的数据底层操作的系统中.POD通常被用在系统的边界处,即指不同系统之间只 ...

  3. 借助 SIMD 数据布局模板和数据预处理提高 SIMD 在动画中的使用效率

    原文链接 简介 为发挥 SIMD1 的最大作用,除了对其进行矢量化处理2外,我们还需作出其他努力.可以尝试为循环添加 #pragma omp simd3,查看编译器是否成功进行矢量化,如果性能有所提升 ...

  4. Google C++ Style Guide

    Background C++ is one of the main development languages used by many of Google's open-source project ...

  5. C语言编程之道--读书笔记

    C语言语法 const int nListNum =sizeof(aPrimeList)/sizeof(unsigned);//计算素数表里元素的个数 1:#define INM_MAX 32767 ...

  6. Google C++ 代码规范

    Google C++ Style Guide   Table of Contents Header Files Self-contained Headers The #define Guard For ...

  7. C++11新特性——大括号初始化

    C++11之前,C++主要有以下几种初始化方式: //小括号初始化 string str("hello"); //等号初始化 string str="hello" ...

  8. C++11:POD数据类型

    版权声明:本文为博主原创文章,未经博主允许不得转载. 目录(?)[+] 啥是POD类型? POD全称Plain Old Data.通俗的讲,一个类或结构体通过二进制拷贝后还能保持其数据不变,那么它就是 ...

  9. c++11 pod类型(了解)

    啥是POD类型? POD全称Plain Old Data.通俗的讲,一个类或结构体通过二进制拷贝后还能保持其数据不变,那么它就是一个POD类型. 平凡的定义 .有平凡的构造函数 .有平凡的拷贝构造函数 ...

随机推荐

  1. windows 10下sublime text3环境的搭建以及配置python开发环境

    1 - 安装Sublime Text 3 到官网下载对应的版本,如下: OS X (10.7 or later is required) Windows - also available as a p ...

  2. 20165237 2017-2018-2 《Java程序设计》第5周学习总结

    20165237 2017-2018-2 <Java程序设计>第5周学习总结 教材学习内容总结 1.内部类:在一个类中定义另一个类:外嵌类:包含内部类的类. 2.内部类的类体中不能声明类变 ...

  3. linux系统内核版本升级

    一.查看Linux内核版本命令(2种方法): 1.cat /proc/version 2.uname -a 二.查看Linux系统版本的命令(3种方法): 1.lsb_release -a 即可列出所 ...

  4. OsWatcher 使用详解

    软件下载地址: https://support.oracle.com/epmos/faces/DocumentDisplay?_afrLoop=520996062954556&id=30113 ...

  5. mac 电脑连接linux 服务器

    Mac 电脑下连接Linux服务器 命令: ssh -p 端口号(22) 用户名@ip OK,输入密码,搞定

  6. FAT文件系统规范v1.03学习笔记---3.根目录区之FAT目录项结构

    1.前言 本文主要是对Microsoft Extensible Firmware Initiative FAT32 File System Specification中文翻译版的学习笔记. 每个FAT ...

  7. python3+selenium入门16-窗口截图

    有时候需要把一些浏览器当前窗口截图下来,比如操作抱错的时候.WebDriver类下.get_screenshot_as_file()方法可窗口截图,需要传入一个截图文件名的路径.window要用\\当 ...

  8. MVC 中Delete 方法报错问题解决方案

    最开始前台ajax提交时代码 function Del(id) { $.ajax({ type: "GET", url: "/Test/Delete", dat ...

  9. 题解-poj3682King Arthur's Birthday Celebration

    Problem poj-3682 题目大意:抛一次硬币有\(p\)的概率得到正面,当有\(n\)次正面时停止,抛第\(i\)次的花费为\(2i-1\),求抛的期望次数和期望花费 Solution 本来 ...

  10. <TCP/IP>Internet地址结构回顾

    本章介绍了Internet中使用的网络层地址,又称IP地址. 要想在网上冲浪,一个设备至少要有一个IP地址(PS:我用赛风FQ的时候,居然自动更换了IP地址,顿时感觉很神奇但是不知道为什么) ***成 ...