std::numeric_limits<T>::is_signed
來自 cppreference.com
static const bool is_signed; |
(C++11 前) | |
static constexpr bool is_signed; |
(C++11 起) | |
std::numeric_limits<T>::is_signed 的值為所有有符號算術型別 T
為 true,無符號型別為 false。此常量對所有特化都有效。
[編輯] 標準特化
T
|
std::numeric_limits<T>::is_signed 的值 |
/* 未特化 */ | false |
bool | false |
char | 實現定義 |
signed char | true |
unsigned char | false |
wchar_t | 實現定義 |
char8_t (C++20起) | false |
char16_t (C++11起) | false |
char32_t (C++11起) | false |
short | true |
unsigned short | false |
int | true |
unsigned int | false |
long | true |
unsigned long | false |
long long (C++11起) | true |
unsigned long long (C++11起) | false |
float | true |
double | true |
long double | true |
[編輯] 示例
執行此程式碼
#include <iostream> #include <iomanip> #include <limits> template<typename T> struct test { test(const char* name, int w = 15) { std::cout << std::left << std::setw(w) << (std::numeric_limits<T>::is_specialized ? name : "non-specialized") << " : " << (std::numeric_limits<T>::is_signed ? "" : "un") << "signed\n"; } }; int main() { test<bool>{"bool"}; test<char>{"char"}; test<wchar_t>{"wchar_t"}; test<char16_t>{"char16_t"}; test<char32_t>{"char32_t"}; test<float>{"float"}; struct delusion{}; test<delusion>{"delusion"}; test<decltype(42)>{"decltype(42)"}; }
可能的輸出
bool : unsigned char : signed wchar_t : signed char16_t : unsigned char32_t : unsigned float : signed non-specialized : unsigned decltype(42) : signed
[編輯] 參閱
(C++11) |
檢查型別是否為有符號算術型別 (類模板) |
[靜態] |
確定整數型別 (公共靜態成員常量) |
[靜態] |
確定精確型別 (公共靜態成員常量) |
[靜態] |
確定表示有限值集合的型別 (公共靜態成員常量) |