<type_traits>

類模板
<type_traits>

std::is_arithmetic

template <class T> struct is_arithmetic;
is_arithmetic 型別

如果 T 是算術型別(即整數型別或浮點型別),則為特徵類。

它繼承自 integral_constant,並根據T是一個算術型別

基本算術型別
整數型別bool
char
char16_t
char32_t
wchar_t
signed char
short int
int
long int
long long int
unsigned char
unsigned short int
unsigned int
unsigned long int
unsigned long long int
浮點型別float
double
long double

所有基本算術型別,以及它們的所有別名(如 cstdint 中的別名),都被此類視為算術型別,包括它們的 constvolatile 限定變體。

列舉在 C++ 中不被視為算術型別(參見 is_enum)。

這是一個複合型別特徵,其行為與以下程式碼相同:
1
2
3
template<class T>
struct is_arithmetic : std::integral_constant < bool,
         is_integral<T>::value || is_floating_point<T>::value > {};

模板引數

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
// is_arithmetic example
#include <iostream>
#include <type_traits>
#include <complex>

int main() {
  std::cout << std::boolalpha;
  std::cout << "is_arithmetic:" << std::endl;
  std::cout << "char: " << std::is_arithmetic<char>::value << std::endl;
  std::cout << "float: " << std::is_arithmetic<float>::value << std::endl;
  std::cout << "float*: " << std::is_arithmetic<float*>::value << std::endl;
  std::cout << "complex<double>: " << std::is_arithmetic<std::complex<double>>::value << std::endl;
  return 0;
}

輸出
is_arithmetic:
char: true
float: true
float*: false
complex<double>: false


另見