Namespaces
Variants

std:: is_arithmetic

From cppreference.net
Metaprogramming library
Type traits
Type categories
(C++11)
(C++11) ( DR* )
(C++11)
(C++11)
is_arithmetic
(C++11)
Type properties
(C++11)
(C++11)
(C++14)
(C++11) (deprecated in C++26)
(C++11) ( until C++20* )
(C++11) (deprecated in C++20)
(C++11)
Type trait constants
Metafunctions
(C++17)
Supported operations
Relationships and property queries
Type modifications
Type transformations
(C++11) (deprecated in C++23)
(C++11) (deprecated in C++23)
(C++11)
(C++11) ( until C++20* ) (C++17)

Compile-time rational arithmetic
Compile-time integer sequences
Definido en el encabezado <type_traits>
template < class T >
struct is_arithmetic ;
(desde C++11)

std::is_arithmetic es un UnaryTypeTrait .

Si T es un tipo aritmético (es decir, un tipo integral o un tipo de punto flotante) o una versión cv-qualified del mismo, proporciona la constante miembro value igual a true . Para cualquier otro tipo, value es false .

Si el programa añade especializaciones para std::is_arithmetic o std::is_arithmetic_v (desde C++17) , el comportamiento es indefinido.

Contenidos

Parámetros de plantilla

T - un tipo a verificar

Plantilla de variable auxiliar

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

Heredado de std:: integral_constant

Constantes miembro

value
[static]
true si T es un tipo aritmético, false en caso contrario
(constante miembro pública estática)

Funciones miembro

operator bool
convierte el objeto a bool , devuelve value
(función miembro pública)
operator()
(C++14)
devuelve value
(función miembro pública)

Tipos miembro

Tipo Definición
value_type bool
type std:: integral_constant < bool , value >

Notas

Los tipos aritméticos son los tipos incorporados para los cuales los operadores aritméticos ( + , - , * , / ) están definidos (posiblemente en combinación con las conversiones aritméticas usuales).

Se proporcionan especializaciones de std::numeric_limits para todos los tipos aritméticos.

Implementación posible

template<class T>
struct is_arithmetic : std::integral_constant<bool,
                                              std::is_integral<T>::value ||
                                              std::is_floating_point<T>::value> {};

Ejemplo

#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() {}

Véase también

comprueba si un tipo es un tipo integral
(plantilla de clase)
comprueba si un tipo es un tipo de punto flotante
(plantilla de clase)