C++ 中的 is_signed 模板
在本文中,我們將討論 C++ STL 中 std::is_signed 模板的原理、語法和示例。
is_ signed 是一個模板,包含在 <type_traits> 標頭檔案中。此模板用於檢查給定的型別 T 是否是有符號型別。
什麼是帶符號型別?
這些是基本算術型別,其中包含符號值。所有算術資料型別都帶有符號或不帶符號。
例如,我們希望以負值顯示值,我們會使用帶符號型別。
例如:-1 是帶符號 int,-1.009 是帶符號浮點數。
預設情況下,所有型別都帶有符號,使它們不帶符號,我們必須在資料型別前加上 unsigned。
語法
template <class T> is_signed;
引數
此模板只能有一個型別為 T 的引數,並檢查 T 是否是有符號型別。
返回值
它返回一個布林值,如果給定的型別是有符號型別,則返回 true,如果給定的型別不是有符號型別,則返回 false。
示例
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
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP