C++ 中的 is_pod 模板
在本文中,我們將介紹 C++ STL 中 std::is_pod 模板的工作原理、語法和示例。
is_ - pod 是一個模板,位於 <type_traits> 標頭檔案中。此模板用於檢查給定的型別 T 是否為 POD(純舊資料)型別。
什麼是 POD(純舊資料)?
純舊資料(POD)型別是指在舊 C 語言中也存在的型別。POD 型別還包括標量型別。POD 類型別是指既是平凡型別(可以在靜態初始化)又是標準佈局(它是一種簡單的資料結構,如結構和聯合)的類型別。
語法
template <class T> is_pod;
引數
此模板可以僅有一個型別為 T 的引數,並檢查給定的型別是否是純舊資料型別。
返回值
它返回一個布林值,如果給定的型別是純舊資料,則為 true,如果給定的型別不是純舊資料,則為 false。
示例
Input: class final_abc{ final_abc(); };
is_pod<final_abc>::value;
Output: False
Input: is_pod<int>::value;
Output: True示例
#include <iostream>
#include <type_traits>
using namespace std;
struct TP_1 {
int var_1;
};
struct TP_2 {
int var_2;
private:
int var_3;
};
struct TP_3 {
virtual void dummy();
};
int main() {
cout << boolalpha;
cout << "checking for is_pod:";
cout << "\nTP_1: " << is_pod<TP_1>::value;
cout << "\nTP_2: " << is_pod<TP_2>::value;
cout << "\nTP_3: " << is_pod<TP_3>::value;
return 0;
}輸出
如果我們執行以上程式碼,它將生成以下輸出 -
checking for is_pod: TP_1: true TP_2: false TP_3: false
示例
#include <iostream>
#include <type_traits>
using namespace std;
class TP_1 {
int var_1;
};
class TP_2 {
int var_2;
private:
int var_3;
};
class TP_3 {
virtual void dummy();
};
int main() {
cout << boolalpha;
cout << "checking for is_pod:";
cout << "\nTP_1: " << is_pod<TP_1>::value;
cout << "\nTP_2: " << is_pod<TP_2>::value;
cout << "\nTP_3: " << is_pod<TP_3>::value;
return 0;
}輸出
如果我們執行以上程式碼,它將生成以下輸出 -
checking for is_pod: TP_1: true TP_2: true TP_3: false
廣告
資料結構
網路
關係型資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP