本文最后更新于:2020年7月2日 晚上

* 在之前的vector扩容问题源代码剖析中,发现源码中对数据类型进行了是否为POD类型的检查,这篇就看看什么是POD。。。→_→ *

了解vector扩容问题请戳传送门——vector扩容问题源代码剖析

详细POD定义说明请戳传送门——POD数据类型解释说明

详细is_pod函数定义说明请戳传送门——is_pod函数定义说明

  • POD,是Plain Old Data的缩写,普通旧数据类型,是C++中的一种数据类型概念

  • POD类型与C编程语言中使用的类型兼容,POD数据类型可以使用C库函数进行操作,也可以使用std::malloc创建,可以使用std::memmove等进行复制,并且可以使用C语言库直接进行二进制形式的数据交换

  • 请注意,C++标准没有使用此名称定义命名的要求或概念。 这是由核心语言定义的类型类别。它被包括在这里作为概念,只是为了一致性。

  • POD数据类型的要求

    • a scalar type(标量类型)

    • a class type (class or struct or union) that is(一个类类型(类、结构体或联合体))

      • 在C++11之前

      • an aggregate type(聚合类型)

      • has no non-static members that are non-POD(没有非POD的非静态成员)

      • has no members of reference type(没有参考类型的成员)

      • has no user-defined copy constructor(没有用户定义的拷贝构造函数)

      • has no user-defined destructor(没有用户定义的析构函数)

      • C++11之后

      • a trivial type(破碎的类型)

      • a standard layout type(标准布局类型)

      • has no non-static members that are non-POD(没有非POD的非静态成员)

    • an array of such type(POD类型的数组)

  • C++中判断数据类型是否为POD的函数:is_pod(C++11)

    //since C++11
    Defined in header <type_traits>
    template< class T >
    struct is_pod;
    • 如果T是PODType(“普通旧数据类型”),即简单和标准布局,则将成员常量值设置为true。 对于任何其他类型,值为false
//输出为:
//true
//false
//false
#include <iostream>
#include <type_traits>
 
struct A {
    int m;
};
 
struct B {
    int m1;
private:
    int m2;
};
 
struct C {
    virtual void foo();
};
 
int main()
{
    std::cout << std::boolalpha;
    std::cout << std::is_pod<A>::value << '\n';
    std::cout << std::is_pod<B>::value << '\n';
    std::cout << std::is_pod<C>::value << '\n';
}

* 其实POD数据类型大概了解一下就行。。。这仅仅是一个概念而已。。。→_→ *