std::is_arithmetic
来自cppreference.com
在标头 <type_traits> 定义 | ||
template<class T > struct is_arithmetic; | (C++11 起) | |
std::is_arithmetic
是一元类型特征(UnaryTypeTrait) 。
如果 T
为算术类型(即整数类型或浮点类型)或其 cv
限定版本,那么提供的成员常量 value
等于 true。对于其它任何类型,value
等于 false。
如果程序添加了 std::is_arithmetic
或 std::is_arithmetic_v
(C++17 起) 的特化,那么行为未定义。
目录 |
[编辑]模板形参
T | - | 要检查的类型 |
[编辑]辅助变量模板
template<class T > constexprbool 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 <atomic>#include <cstddef>#include <type_traits> class A {}; enumclass B :int{ e }; static_assert( std::is_arithmetic_v<bool>==true and std::is_arithmetic_v<char>==true and std::is_arithmetic_v<charconst>==true and std::is_arithmetic_v<int>==true and std::is_arithmetic_v<intconst>==true and std::is_arithmetic_v<float>==true and std::is_arithmetic_v<floatconst>==true and std::is_arithmetic_v<std::size_t>==true and std::is_arithmetic_v<char&>==false and std::is_arithmetic_v<char*>==false and std::is_arithmetic_v<int&>==false and std::is_arithmetic_v<int*>==false and std::is_arithmetic_v<float&>==false and std::is_arithmetic_v<float*>==false and std::is_arithmetic_v<A>==false and std::is_arithmetic_v<B>==false and std::is_arithmetic_v<decltype(B::e)>==false and std::is_arithmetic_v<std::byte>==false and std::is_arithmetic_v<std::atomic_int>==false); int main(){}
[编辑]参阅
(C++11) | 检查类型是否为整数类型 (类模板) |
(C++11) | 检查类型是否为浮点数类型 (类模板) |