std::is_arithmetic
来自 cppreference.cn
定义于头文件 <type_traits> |
||
template< class T > struct is_arithmetic; |
(since C++11) | |
std::is_arithmetic
是一个 UnaryTypeTrait。
如果 T
是算术类型(即,整型或浮点型)或其 cv 限定版本,则提供等于 true 的成员常量 value
。对于任何其他类型, value
为 false。
如果程序为 std::is_arithmetic
或 std::is_arithmetic_v
(since C++17) 添加特化,则行为未定义。
目录 |
[edit] 模板参数
T | - | 要检查的类型 |
[edit] 辅助变量模板
template< class T > constexpr bool is_arithmetic_v = is_arithmetic<T>::value; |
(since C++17) | |
继承自 std::integral_constant
成员常量
value [静态] |
true 如果 T 是算术类型, false 否则(公共静态成员常量) |
成员函数
operator bool |
将对象转换为 bool,返回 value (公共成员函数) |
operator() (C++14) |
返回 value (公共成员函数) |
成员类型
类型 | 定义 |
value_type
|
bool |
type
|
std::integral_constant<bool, value> |
[edit] 注解
算术类型是为其定义了 算术运算符 (+
, -
, *
, /
) 的内建类型(可能与通常的算术转换结合使用)。
为所有算术类型提供了 std::numeric_limits 的特化。
[edit] 可能的实现
template<class T> struct is_arithmetic : std::integral_constant<bool, std::is_integral<T>::value || std::is_floating_point<T>::value> {}; |
[edit] 示例
运行此代码
#include <atomic> #include <cstddef> #include <type_traits> class A {}; enum class B : int { e }; static_assert( std::is_arithmetic_v<bool> == true and std::is_arithmetic_v<char> == true and std::is_arithmetic_v<char const> == true and std::is_arithmetic_v<int> == true and std::is_arithmetic_v<int const> == true and std::is_arithmetic_v<float> == true and std::is_arithmetic_v<float const> == 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() {}
[edit] 参见
(C++11) |
检查类型是否为整型 (class template) |
(C++11) |
检查类型是否为浮点型 (class template) |