<type_traits>

類模板
<type_traits>

std::is_pod

template <class T> struct is_pod;
POD 型別判定

型別特徵類,用於判斷T是否為POD型別。

一個POD型別(Plain Old Data的縮寫)是其特性由C語言中的一種資料型別支援的型別,可以是被cv限定的,也可以是沒有被cv限定的。這包括標量型別、POD類以及任何此類型別的陣列。

一個POD類是一個既平凡(只能進行靜態初始化)又標準佈局(具有簡單的資料結構)的類,因此它在很大程度上僅限於具有與C語言中宣告的C資料結構相容的類的特性struct聯合體在該語言中,即使在它們的宣告中可以使用擴充套件的C++語法,並且可以具有成員函式。

is_pod繼承自integral_constant,其值為true_typefalse_type,取決於T是否為POD型別。

模板引數

T
一個完整型別,或者void(可能被cv限定),或一個未知界限的陣列,其元素型別是完整的。

成員型別

繼承自 integral_constant
成員型別定義
value_typebool
型別true_type 或 false_type

成員常量

繼承自 integral_constant
成員常量定義
truefalse

成員函式

繼承自 integral_constant

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// is_pod example
#include <iostream>
#include <type_traits>

struct A { int i; };            // C-struct (POD)
class B : public A {};          // still POD (no data members added)
struct C : B { void fn(){} };   // still POD (member function)
struct D : C { D(){} };         // no POD (custom default constructor)

int main() {
  std::cout << std::boolalpha;
  std::cout << "is_pod:" << std::endl;
  std::cout << "int: " << std::is_pod<int>::value << std::endl;
  std::cout << "A: " << std::is_pod<A>::value << std::endl;
  std::cout << "B: " << std::is_pod<B>::value << std::endl;
  std::cout << "C: " << std::is_pod<C>::value << std::endl;
  std::cout << "D: " << std::is_pod<D>::value << std::endl;
  return 0;
}

輸出
is_pod:
int: true
A: true
B: true
C: true
D: false


另見