ในบทความนี้ เราจะพูดถึงการทำงาน ไวยากรณ์ และตัวอย่าง std::is_unsigned template ใน C++ STL
is_unsigned เป็นเทมเพลตที่อยู่ภายใต้ไฟล์ส่วนหัว
ประเภทข้อมูลที่ไม่ได้ลงนามใน C++ คืออะไร
ประเภทข้อมูลที่ไม่ได้ลงนามคือประเภทที่เราใช้โดยรู้ว่าค่าต่างๆ จะไม่ติดลบ เช่น เลขม้วน รหัสของตัวเลขสุ่ม ฯลฯ
ในการทำให้ประเภทเป็น unsigned เราใช้คีย์เวิร์ด unsigned เป็นคำนำหน้าของประเภทข้อมูล เช่น −
ไม่ได้ลงชื่อ
โฟลตที่ไม่ได้ลงนาม;
ไวยากรณ์
template <class T>is_unsigned;
พารามิเตอร์
เทมเพลตมีได้เฉพาะพารามิเตอร์ประเภท T และตรวจสอบว่า T เป็นประเภทที่ไม่ได้ลงชื่อหรือไม่
คืนค่า
ส่งคืนค่าบูลีนเป็นค่าจริงหากประเภทที่กำหนดเป็นประเภทที่ไม่ได้ลงนาม และเป็นเท็จหากประเภทที่ระบุไม่ใช่ประเภทที่ไม่ได้ลงนาม
ตัวอย่าง
Input: is_unsigned<unsigned int>::value; Output: True Input: is_unsigned<int>::value; Output: False
ตัวอย่าง
#include <iostream>
#include <type_traits>
using namespace std;
class TP {
};
enum TP_1 : int {};
enum class TP_2 : int {};
int main() {
cout << boolalpha;
cout << "checking for is_unsigned:";
cout << "\nint:" << is_unsigned<int>::value;
cout << "\nTP:" << is_unsigned<TP>::value;
cout << "\nTP_1:" << is_unsigned<TP_1>::value;
cout << "\nTP_2:" << is_unsigned<TP_2>::value;
return 0;
} ผลลัพธ์
หากเราเรียกใช้โค้ดข้างต้น มันจะสร้างผลลัพธ์ต่อไปนี้ -
checking for is_unsigned: Int: false TP: false TP_1: false TP_2: false
ตัวอย่าง
#include <iostream>
#include <type_traits>
using namespace std;
int main() {
cout << boolalpha;
cout << "checking for is_unsigned:";
cout << "\nfloat:" << is_unsigned<float>::value;
cout << "\nSigned int:" << is_unsigned<signed int>::value;
cout << "\nUnsigned int:" << is_unsigned<unsigned int>::value;
cout << "\ndouble:" << is_unsigned<double>::value;
return 0;
} ผลลัพธ์
หากเราเรียกใช้โค้ดข้างต้น มันจะสร้างผลลัพธ์ต่อไปนี้ -
checking for is_signed: Float: false Signed int: false Unsigned int: true Double: false