std::numeric_limits<T>:: is_signed
From cppreference.net
<
cpp
|
types
|
numeric limits
C++
Utilities library
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Type support
| Basic types | |||||||||||||||||||||
| Fixed width integer types (C++11) | |||||||||||||||||||||
| Fixed width floating-point types (C++23) | |||||||||||||||||||||
|
|||||||||||||||||||||
| Numeric limits | |||||||||||||||||||||
| C numeric limits interface | |||||||||||||||||||||
| Runtime type information | |||||||||||||||||||||
|
|||||||||||||||||||||
std::numeric_limits
| Static constants | ||||
|
numeric_limits::is_signed
|
||||
|
(C++11)
|
||||
| Static member functions | ||||
|
(C++11)
|
||||
| Helper types | ||||
|
static
const
bool
is_signed
;
|
(C++11 前) | |
|
static
constexpr
bool
is_signed
;
|
(C++11 起) | |
对于所有有符号算术类型
T
,
std::
numeric_limits
<
T
>
::
is_signed
的值为
true
;对于无符号类型,其值为
false
。该常量对所有特化版本均有意义。
标准特化
T
|
std:: numeric_limits < T > :: is_signed 的值 |
| /* non-specialized */ | false |
| bool | false |
| char | 由实现定义 |
| signed char | true |
| unsigned char | false |
| wchar_t | 由实现定义 |
| char8_t (since C++20) | false |
| char16_t (since C++11) | false |
| char32_t (since C++11) | false |
| short | true |
| unsigned short | false |
| int | true |
| unsigned int | false |
| long | true |
| unsigned long | false |
| long long (since C++11) | true |
| unsigned long long (since 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)
|
检查类型是否为有符号算术类型
(类模板) |
|
[static]
|
标识整数类型
(公开静态成员常量) |
|
[static]
|
标识精确类型
(公开静态成员常量) |
|
[static]
|
标识表示有限值集的类型
(公开静态成员常量) |