<type_traits>

類模板
<type_traits>

std::is_signed

template <class T> struct is_signed;
有符號型別

如果 T 是有符號的 算術型別,則該特性類標識此情況。

它繼承自 integral_constant,並根據T被視為有符號。

一個型別,T如果該型別是算術型別(即,基本 整型浮點型 型別),並且滿足以下條件,則該特性類認為該型別是 有符號 的。
1
T(-1) < T(0)

模板引數

T
一個型別。

成員型別

繼承自 integral_constant
成員型別定義
value_typebool
型別true_type 或 false_type

成員常量

繼承自 integral_constant
成員常量定義
truefalse

成員函式

繼承自 integral_constant

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// is_signed example
#include <iostream>
#include <type_traits>

struct A { };
enum class B : int { x,y,z };

int main() {
  std::cout << std::boolalpha;
  std::cout << "is_signed:" << std::endl;
  std::cout << "int: " << std::is_signed<int>::value << std::endl;
  std::cout << "float: " << std::is_signed<float>::value << std::endl;
  std::cout << "unsigned long: " << std::is_signed<unsigned long>::value << std::endl;
  std::cout << "A: " << std::is_signed<A>::value << std::endl;
  std::cout << "B: " << std::is_signed<B>::value << std::endl;
  return 0;
}

輸出
is_signed:
int: true
float: true
unsigned long: false
A: false
B: false


另見