类型特性
类型特性定义一个编译时基于模板的结构,以查询或修改类型的属性。
试图特化定义于 <type_traits> 头文件的模板导致未定义行为,除了 std::common_type 可依照其所描述特化。
定义于<type_traits>头文件的模板可以用不完整类型实例化,除非另外有指定,尽管通常禁止以不完整类型实例化标准库模板。
类型修改
类型修改模板通过应用修改到模板参数,创建新类型定义。结果类型可以通过成员 typedef type 访问。
使给定的整型类型有符号
std::make_unsigned
template< class T > | (C++11 起) |
若 T
是整数(除 bool )或枚举类型,则提供是对应 T
的有符号整数类型的成员 typedef type
,它拥有相同的 cv 限定符。
否则,行为未定义。
成员类型
名称 | 定义 |
type | 对应 T 的有符号整数类型 |
辅助类型
template< class T > | (C++14 起) |
可能的实现
/// make_signed
template<typename _Tp>
struct make_signed
{ typedef typename __make_signed_selector<_Tp>::__type type; };
// Integral, but don't define.
template<>
struct make_signed<bool>;
#if __cplusplus > 201103L
/// Alias template for make_signed
template<typename _Tp>
using make_signed_t = typename make_signed<_Tp>::type;
/// Alias template for make_unsigned
template<typename _Tp>
using make_unsigned_t = typename make_unsigned<_Tp>::type;
#endif
调用示例
#include <iostream>
#include <type_traits>
int main()
{
typedef std::make_signed<char>::type char_type;
typedef std::make_signed<int>::type int_type;
typedef std::make_signed<volatile long>::type long_type;
typedef std::make_signed<long long>::type long_long_type;
typedef std::make_signed<short>::type short_type;
typedef std::make_signed<int8_t>::type int8_t_type;
typedef std::make_signed<int16_t>::type int16_t_type;
typedef std::make_signed<int32_t>::type int32_t_type;
typedef std::make_signed<int64_t>::type int64_t_type;
std::cout << std::boolalpha;
std::cout << "std::is_same<std::make_signed<char>::type, signed char>::value: "
<< std::is_same<char_type, signed char>::value << std::endl;
std::cout << "std::is_same<std::make_signed<int>::type, signed int>::value: "
<< std::is_same<int_type, signed int>::value << std::endl;
std::cout << "std::is_same<std::make_signed<volatile long>::type, volatile signed long>::value: "
<< std::is_same<long_type, volatile signed long>::value << std::endl;
std::cout << "std::is_same<std::make_signed<long long>::type, signed long long>::value: "
<< std::is_same<long_long_type, signed long long>::value << std::endl;
std::cout << "std::is_same<std::make_signed<short>::type, signed short>::value: "
<< std::is_same<short_type, signed short>::value << std::endl;
std::cout << "std::is_same<std::make_signed<int8_t_type>::type, int8_t>::value: "
<< std::is_same<int8_t_type, int8_t>::value << std::endl;
std::cout << "std::is_same<std::make_signed<int16_t_type>::type, int16_t>::value: "
<< std::is_same<int16_t_type, int16_t>::value << std::endl;
std::cout << "std::is_same<std::make_signed<int32_t_type>::type, int32_t>::value: "
<< std::is_same<int32_t_type, int32_t>::value << std::endl;
std::cout << "std::is_same<std::make_signed<int64_t_type>::type, int64_t>::value: "
<< std::is_same<int64_t_type, int64_t>::value << std::endl;
return 0;
}
输出
std::is_same<std::make_unsigned<char>::type, unsigned char>::value: true
std::is_same<std::make_unsigned<int>::type, unsigned int>::value: true
std::is_same<std::make_unsigned<volatile long>::type, volatile unsigned long>::value: true
std::is_same<std::make_unsigned<long long>::type, unsigned long long>::value: true
std::is_same<std::make_unsigned<short>::type, unsigned short>::value: true
std::is_same<std::make_unsigned<int8_t_type>::type, uint8_t>::value: true
std::is_same<std::make_unsigned<int16_t_type>::type, uint16_t>::value: true
std::is_same<std::make_unsigned<int32_t_type>::type, uint32_t>::value: true
std::is_same<std::make_unsigned<int64_t_type>::type, uint64_t>::value: true