std::numeric_limits<T>::is_signed
来自 cppreference.cn
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) |
检查类型是否为有符号算术类型 (类模板) |
| [静态] |
确定整数类型 (公共静态成员常量) |
| [静态] |
确定精确类型 (公共静态成员常量) |
| [静态] |
确定表示有限值集合的类型 (公共静态成员常量) |