std::is_arithmetic

定义于头文件 <type_traits>
template< class T >
struct is_arithmetic;
(C++11 起)

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

目录

模板形参

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

参阅

(C++11)
检查类型是否为整型
(类模板)
检查类型是否是浮点类型
(类模板)

版本历史

  • (当前 | 先前 2017年11月17日 (五) 08:51Fruderica讨论 | 贡献. . (2,890字节) (+390). . (撤销)
  • 当前 | 先前 2017年7月25日 (二) 05:52Fruderica讨论 | 贡献. . (2,500字节) (+489). . (撤销)
  • 当前 | 先前 2014年10月26日 (日) 18:12P12bot讨论 | 贡献 . . (2,011字节) (0). . (Fix some translations) (撤销)
  • 当前 | 先前 2013年7月2日 (二) 11:39P12bot讨论 | 贡献 . . (2,011字节) (-83). . (Use {{lc}}. Update links. Various fixes.) (撤销)
  • 当前 | 先前 2012年11月2日 (五) 16:52P12bot讨论 | 贡献 . . (2,094字节) (+249). . (r2.7.3) (机器人添加:de, en, es, fr, it, ja, pt, ru) (撤销)
  • 当前 | 先前 2012年10月27日 (六) 11:38P12讨论 | 贡献 . . (1,845字节) (0). . (1个修订: Translate from the English version) (撤销)
  • 当前 | 先前 2012年10月26日 (五) 06:00TranslationBot讨论 | 贡献. . (1,845字节) (-31). . (Translated from the English version using Google Translate) (撤销)
  • 当前 | 先前 2012年10月26日 (五) 03:33P12讨论 | 贡献 . . (1,876字节) (0). . (1个修订: Translate from the English version) (撤销)
  • 当前 | 先前) 2012年10月25日 (四) 12:00TranslationBot讨论 | 贡献. . (1,876字节) (+1,876). . (Translated from the English version using Google Translate)