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
Определено в заголовочном файле <type_traits>
template < class T >
struct is_arithmetic ;
(начиная с C++11)

std::is_arithmetic является UnaryTypeTrait .

Если T является арифметическим типом (то есть целочисленным типом или типом с плавающей запятой) или его cv-qualified версией, предоставляет константу-член value равную true . Для любого другого типа value равно false .

Если программа добавляет специализации для std::is_arithmetic или std::is_arithmetic_v (начиная с C++17) , поведение не определено.

Содержание

Параметры шаблона

T - тип для проверки

Шаблон вспомогательной переменной

template < class T >
constexpr bool is_arithmetic_v = is_arithmetic < T > :: value ;
(начиная с C++17)

Унаследовано от std:: integral_constant

Константы-члены

value
[static]
true если T является арифметическим типом, 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> {};
**Примечание:** Весь код C++ внутри тегов `
` и `` оставлен без изменений, как и требовалось. HTML-разметка и атрибуты также сохранены в оригинальном виде.

Пример

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

Смотрите также

проверяет, является ли тип целочисленным типом
(шаблон класса)
проверяет, является ли тип типом с плавающей запятой
(шаблон класса)