什么是POD数据类型?

原文链接:http://blog.csdn.net/kongkongkkk/article/details/77414410


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

了解vector扩容问题请戳传送门——vector扩容问题源代码剖析 http://blog.csdn.net/kongkongkkk/article/details/77320794

详细POD定义说明请戳传送门——POD数据类型解释说明 http://en.cppreference.com/w/cpp/concept/PODType

详细is_pod函数定义说明请戳传送门——is_pod函数定义说明http://en.cppreference.com/w/cpp/types/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;
       
       
    • 1
    • 2
    • 3
    • 4
    • 如果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';
}
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28

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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值