1 std::is_arithmetic

//头文件 <type_traits>
		
template< class T >
struct is_arithmetic;
		(since C++11)

        功能:如果 T 是算术类型(即整数类型或浮点类型)或其 cv 限定版本(cv-qualified version thereof),则提供等于 true 的成员常量值。 对于任何其他类型,值为 false。

        拓展定义:

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

        一种实现方法:

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
        << "A:           " << std::is_arithmetic_v<A>           << '\n' // false
        << "bool:        " << std::is_arithmetic_v<bool>        << '\n' // true
        << "int:         " << std::is_arithmetic_v<int>         << '\n' // true
        << "int const:   " << std::is_arithmetic_v<int const>   << '\n' // true
        << "int &:       " << std::is_arithmetic_v<int&>        << '\n' // false
        << "int *:       " << std::is_arithmetic_v<int*>        << '\n' // false
        << "float:       " << std::is_arithmetic_v<float>       << '\n' // true
        << "float const: " << std::is_arithmetic_v<float const> << '\n' // true
        << "float &:     " << std::is_arithmetic_v<float&>      << '\n' // false
        << "float *:     " << std::is_arithmetic_v<float*>      << '\n' // false
        << "char:        " << std::is_arithmetic_v<char>        << '\n' // true
        << "char const:  " << std::is_arithmetic_v<char const>  << '\n' // true
        << "char &:      " << std::is_arithmetic_v<char&>       << '\n' // false
        << "char *:      " << std::is_arithmetic_v<char*>       << '\n' // false
        ;
}

@meng