CppDS.com

C++ 98 11 14 17 20 手册

std::is_arithmetic

来自cppreference.com
< cpp‎ | types
 
 
工具库
通用工具
日期和时间
函数对象
格式化库 (C++20)
(C++11)
关系运算符 (C++20 中弃用)
整数比较函数
(C++20)
swap 与类型运算
(C++14)
(C++11)
(C++11)
(C++11)
(C++17)
常用词汇类型
(C++11)
(C++17)
(C++17)
(C++17)
(C++17)

初等字符串转换
(C++17)
(C++17)
 
类型支持
基本类型
基础类型
定宽整数类型 (C++11)
数值极限
C 数值极限接口
运行时类型信息
类型特性
类型类别
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
is_arithmetic
(C++11)
类型属性
(C++11)
(C++11)
(C++14)
(C++11)
(C++11)(C++20 前)
(C++11)(C++20 中弃用)
(C++11)
类型特性常量
元函数
(C++17)
常量求值语境
受支持操作
关系与属性查询
类型修改
(C++11)(C++11)(C++11)
类型变换
(C++11)
(C++11)
(C++17)
(C++11)(C++20 前)(C++17)
 
定义于头文件 <type_traits>
template< class T >
struct is_arithmetic;
(C++11 起)

T 为算术类型(即整数类型或浮点类型)或其 cv 限定版本,则提供等于 true 的成员常量 value 。对于任何其他类型, valuefalse

添加 is_arithmetic is_arithmetic_v (C++17 起) 的特化的程序行为未定义。

模板形参

T - 要检查的类型

辅助变量模板

template< class T >
inline constexpr bool is_arithmetic_v = is_arithmetic<T>::value;
(C++17 起)

继承自 std::integral_constant

成员常量

value
[静态]
T 为算术类型则为 true ,否则为 false
(公开静态成员常量)

成员函数

operator bool
转换对象为 bool ,返回 value
(公开成员函数)
operator()
(C++14)
返回 value
(公开成员函数)

成员类型

 
类型 定义
value_type bool
type std::integral_constant<bool, value>

注解

算术类型是算术运算符( + 、 - 、 * 、 / )为之定义的内建类型(可能与通常算术转换结合)

为所有算术类型提供 std::numeric_limits 的特化。

可能的实现

template< class T >
struct is_arithmetic : std::integral_constant<bool,
                                              std::is_integral<T>::value ||
                                              std::is_floating_point<T>::value> {};

示例

#include <iostream>
#include <type_traits>
 
class A {};
 
int main() 
{
    std::cout << std::boolalpha;
    std::cout << "A:           " <<  std::is_arithmetic<A>::value << '\n';
    std::cout << "bool:        " <<  std::is_arithmetic<bool>::value << '\n';
    std::cout << "int:         " <<  std::is_arithmetic<int>::value << '\n';
    std::cout << "int const:   " <<  std::is_arithmetic<int const>::value << '\n';
    std::cout << "int &:       " <<  std::is_arithmetic<int&>::value << '\n';
    std::cout << "int *:       " <<  std::is_arithmetic<int*>::value << '\n';
    std::cout << "float:       " <<  std::is_arithmetic<float>::value << '\n';
    std::cout << "float const: " <<  std::is_arithmetic<float const>::value << '\n';
    std::cout << "float &:     " <<  std::is_arithmetic<float&>::value << '\n';
    std::cout << "float *:     " <<  std::is_arithmetic<float*>::value << '\n';
    std::cout << "char:        " <<  std::is_arithmetic<char>::value << '\n';
    std::cout << "char const:  " <<  std::is_arithmetic<char const>::value << '\n';
    std::cout << "char &:      " <<  std::is_arithmetic<char&>::value << '\n';
    std::cout << "char *:      " <<  std::is_arithmetic<char*>::value << '\n';
}

输出:

A:           false
bool:        true
int:         true
int const:   true
int &:       false
int *:       false
float:       true
float const: true
float &:     false
float *:     false
char:        true
char const:  true
char &:      false
char *:      false

参阅

检查类型是否为整型
(类模板)
检查类型是否是浮点类型
(类模板)
关闭