什麼是POD資料型別?
*在之前的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
- 如果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資料型別大概瞭解一下就行。。。這僅僅是一個概念而已。。。→_→*