ในบทความนี้ เราจะพูดถึงการทำงาน ไวยากรณ์ และตัวอย่างของเทมเพลต std::is_pod ใน C++ STL
is_ pod เป็นเทมเพลตที่อยู่ภายใต้ไฟล์ส่วนหัว
POD(ข้อมูลเก่าธรรมดา) คืออะไร
ประเภทข้อมูลเก่าธรรมดา (POD) เป็นประเภทที่อยู่ในภาษา C แบบเก่าเช่นกัน ประเภท POD ยังรวมถึงประเภทสเกลาร์ด้วย ประเภทคลาส POD คือประเภทคลาสที่มีทั้งแบบไม่สำคัญ (ซึ่งสามารถเริ่มต้นแบบสแตติกได้) และเลย์เอาต์มาตรฐาน (ซึ่งเป็นโครงสร้างข้อมูลอย่างง่าย เช่น struct และ union)
ไวยากรณ์
template <class T> is_pod;
พารามิเตอร์
เทมเพลตสามารถมีได้เฉพาะพารามิเตอร์ประเภท T และตรวจสอบว่าประเภทที่กำหนดเป็นประเภทข้อมูลแบบธรรมดาหรือไม่
คืนค่า
ส่งคืนค่าบูลีนเป็นค่าจริงหากประเภทที่กำหนดเป็นข้อมูลเก่าแบบธรรมดา และเป็นเท็จหากประเภทที่กำหนดไม่ใช่ข้อมูลแบบธรรมดา
ตัวอย่าง
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