std::numeric_limits<T>::is_signed
来自cppreference.com
< cpp | types | numeric limits
staticconstbool is_signed; | (C++11 前) | |
staticconstexprbool is_signed; | (C++11 起) | |
std::numeric_limits<T>::is_signed 的值对所有有符号算数类型 T
是 true,而对无符号类型是 false。此常量对所有特化都有意义。
[编辑]标准特化
T | std::numeric_limits<T>::is_signed 的值 |
/* 未特化 */ | false |
bool | false |
char | 由实现定义 |
signedchar | true |
unsignedchar | false |
wchar_t | 由实现定义 |
char8_t(C++20 起) | false |
char16_t(C++11 起) | false |
char32_t(C++11 起) | false |
short | true |
unsignedshort | false |
int | true |
unsignedint | false |
long | true |
unsignedlong | false |
longlong(C++11 起) | true |
unsignedlonglong(C++11 起) | false |
float | true |
double | true |
longdouble | true |
[编辑]示例
运行此代码
#include <iostream>#include <iomanip>#include <limits> template<typename T>struct test { test(constchar* name, int w =15){std::cout<<std::left<<std::setw(w)<<(std::numeric_limits<T>::is_specialized? name :"未特化")<<":"<<(std::numeric_limits<T>::is_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 :无符号 char :有符号 wchar_t :有符号 char16_t :无符号 char32_t :无符号 float :有符号 non-specialized:无符号 decltype(42) :有符号
[编辑]参阅
(C++11) | 检查类型是否为有符号算术类型 (类模板) |
[静态] | 鉴别整数类型 (公开静态成员常量) |
[静态] | 鉴别准确表示的类型 (公开静态成员常量) |
[静态] | 鉴别表示有限值集合的类型 (公开静态成员常量) |