<type_traits>

类模板
<type_traits>

std::make_signed

template <class T> struct make_signed;
Make signed
Obtains the signed type corresponding toT, keeping any cv-qualifiers.

转换后的类型别名为成员类型make_signed::type如下
  • 如果已知Tis an unsigned integral type, this is the corresponding signed type.
  • 如果已知Tis already a signed integral type, this isT不变。
  • 如果已知Tis an enumeration type, this is the smallest signed integer type with the same size asT(as persizeofoperator).

Notice that this class merely obtains a type using another type as model, but it does not transform values or objects between those types. To explicitly cast an integral type to its signed equivalent, you can usestatic_cast.

模板参数

T
An integer type (exceptbool), or an enumeration type.

成员类型

成员类型定义
类型Signed type corresponding toT.

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// make_signed
#include <iostream>
#include <type_traits>

enum ENUM1 {a,b,c};
enum class ENUM2 : unsigned char {x,y,z};

int main() {
  typedef std::make_signed<int>::type A;                // int
  typedef std::make_signed<unsigned>::type B;           // int
  typedef std::make_signed<const unsigned>::type C;     // const int
  typedef std::make_signed<ENUM1>::type D;              // int
  typedef std::make_signed<ENUM2>::type E;              // signed char

  std::cout << std::boolalpha;
  std::cout << "typedefs of int:" << std::endl;
  std::cout << "A: " << std::is_same<int,A>::value << std::endl;
  std::cout << "B: " << std::is_same<int,B>::value << std::endl;
  std::cout << "C: " << std::is_same<int,C>::value << std::endl;
  std::cout << "D: " << std::is_same<int,D>::value << std::endl;
  std::cout << "E: " << std::is_same<int,E>::value << std::endl;

  return 0;
}

输出
typedefs of int:
A: true
B: true
C: false
D: true
E: false


另见