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