std::numeric_limits<T>::is_signed
来自 cppreference.cn
< cpp | types | numeric limits
static const bool is_signed; |
(until C++11) | |
static constexpr bool is_signed; |
(since 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) |
检查类型是否为有符号算术类型 (类模板) |
[静态] |
识别整数类型 (公共静态成员常量) |
[静态] |
识别精确类型 (公共静态成员常量) |
[静态] |
识别表示有限值集的类型 (公共静态成员常量) |