types_is_arithmetic

types_is_arithmetic (산술 타입 판별)

std::is_arithmetic은 주어진 타입이 산술 타입(정수 타입 또는 부동소수점 타입)인지 확인하는 타입 특질(Type Trait)이에요. 이 페이지에서는 이 템플릿의 정의, 사용법, 예제를 해요체로 설명할게요.

출처: cppreference

본문

개요

<type_traits> 헤더에 정의되어 있어요.

Defined in header <type_traits>
template < class T > struct is_arithmetic ; (since C++11)

std::is_arithmetic은 단항 타입 특질(UnaryTypeTrait)이에요.

만약 T가 산술 타입(즉, 정수 타입 또는 부동소수점 타입)이거나 그 cv-한정 버전이라면, 멤버 상수 valuetrue가 돼요. 다른 모든 타입에 대해서는 valuefalse예요.

프로그램이 std::is_arithmetic 또는 std::is_arithmetic_v(C++17부터)에 대한 특수화를 추가하면 동작이 정의되지 않아요.

템플릿 매개변수

T - 확인할 타입

헬퍼 변수 템플릿

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

std::integral_constant에서 상속받은 멤버

멤버 상수

value [static] T가 산술 타입이면 true, 아니면 false (공용 정적 멤버 상수)

멤버 함수

operator bool 객체를 bool로 변환하고 value를 반환해요 (공용 멤버 함수)
operator() (C++14) value를 반환해요 (공용 멤버 함수)
--- ---

멤버 타입

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

같이 보기

is_integral (C++11) 타입이 정수 타입인지 확인해요 (클래스 템플릿) [edit]
is_floating_point (C++11) 타입이 부동소수점 타입인지 확인해요 (클래스 템플릿) [edit]
is_arithmetic_type (C++26) 리플렉션이 산술 타입을 나타내는지 확인해요 (함수) [edit]

더 알아보기 (Learn more)

cppreference