c++(3):std::is_arithmetic

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

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值