std::make_signed
来自cppreference.com
在标头 <type_traits> 定义 | ||
template<class T > struct make_signed; | (C++11 起) | |
若 T
是整数(除 bool)或枚举类型,则提供 T
的对应有符号整数类型的成员 typedef type
,它拥有相同的 cv 限定符。
若 T
为有符号或无符号的 char、short、int、long、longlong,则提供此列表中的 T
的对应有符号类型。
若 T
为枚举类型或 char、wchar_t、char8_t(C++20 起)、char16_t、char32_t,则提供与 T
有相同 sizeof
的有最小等级的有符号整数类型。
否则,行为未定义。 | (C++20 前) |
否则,程序非良构。 | (C++20 起) |
如果程序添加了 std::make_signed
的特化,那么行为未定义。
目录 |
[编辑]成员类型
名称 | 定义 |
type | T 的对应有符号整数类型 |
[编辑]辅助类型
template<class T > using make_signed_t =typename make_signed<T>::type; | (C++14 起) | |
[编辑]示例
运行此代码
#include <type_traits> enumstruct E :unsignedshort{}; int main(){using char_type = std::make_signed_t<unsignedchar>;using int_type = std::make_signed_t<unsignedint>;using long_type = std::make_signed_t<volatileunsignedlong>;using enum_type = std::make_signed_t<E>; static_assert(std::is_same_v<char_type, signedchar> and std::is_same_v<int_type, signedint> and std::is_same_v<long_type, volatilesignedlong> and std::is_same_v<enum_type, signedshort>);}
[编辑]参阅
(C++11) | 检查类型是否为有符号算术类型 (类模板) |
(C++11) | 检查类型是否为无符号算术类型 (类模板) |
(C++11) | 获取整数类型的对应无符号类型 (类模板) |