types_is_scalar

types_is_scalar (스칼라 타입 판별)

std::is_scalar는 주어진 타입 T가 스칼라 타입인지 확인하는 C++11부터 제공되는 타입 특성(type trait)이에요. 스칼라 타입에는 산술 타입, 열거형, 포인터, 멤버 포인터, std::nullptr_t 등이 포함돼요. 이 페이지에서는 std::is_scalar의 정의와 사용법, 구현 예시를 살펴봐요.

출처: cppreference

본문

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

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

std::is_scalar는 단항 타입 특성(UnaryTypeTrait)이에요.
만약 T가 스칼라 타입이라면 멤버 상수 valuetrue가 돼요. 그 외의 모든 타입에 대해서는 valuefalse예요.
프로그램에서 std::is_scalar나 std::is_scalar_v에 대한 특수화를 추가하면 동작이 정의되지 않아요.

템플릿 매개변수

T - 확인할 타입

헬퍼 변수 템플릿

template < class T > constexpr bool is_scalar_v = is_scalar < 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 >

참고 사항

C++ 메모리 모델에서 각각의 개별 메모리 위치는, 언어 기능이 사용하는 숨겨진 메모리 위치(예: 가상 테이블 포인터)를 포함하여 모두 스칼라 타입을 가지거나(또는 0이 아닌 길이의 인접 비트 필드 시퀀스예요). 표현식 평가에서 부수 효과의 순서, 스레드 간 동기화, 의존성 순서는 모두 개별 스칼라 객체를 기준으로 정의돼요.

가능한 구현

| template < class T > struct is_scalar : std :: integral_constant < bool , std :: is_arithmetic < T >:: value || std :: is_enum < T >:: value || std :: is_pointer < T >:: value || std :: is_member_pointer < T >:: value || std :: is_null_pointer < T >:: value #if __cpp_impl_reflection > 0 || std :: is_reflection_v < T > #endif > {}; | |---|

예제

#include <iostream>
#include <type_traits>
#include <typeinfo>
#include <utility>

template<typename Head, typename... Tail>
void are_scalars(Head&& head, Tail&&... tail)
{
    using T = std::decay_t<decltype(head)>;
    
    std::cout << typeid(T).name() << " is "
              << (std::is_scalar_v<T> ? "" : "not ")
              << "a scalar\n";
    
    if constexpr (sizeof... (Tail))
    {
        are_scalars(std::forward<decltype(tail)>(tail)...);
    }
}

int main()
{
    struct S { int m; } s;
    int S::* mp = &S::m;
    enum class E { e };
    
    are_scalars(42, 3.14, E::e, "str", mp, nullptr, s);
}

Possible output:

int is a scalar
double is a scalar
main::E is a scalar
char const* is a scalar
int main::S::* is a scalar
nullptr is a scalar
main::S is not a scalar

같이 보기

is_arithmetic (C++11) 타입이 산술 타입인지 확인해요 (클래스 템플릿)
is_enum (C++11) 타입이 열거형 타입인지 확인해요 (클래스 템플릿)
is_pointer (C++11) 타입이 포인터 타입인지 확인해요 (클래스 템플릿)
is_member_pointer (C++11) 타입이 비정적 멤버 함수나 객체를 가리키는 포인터인지 확인해요 (클래스 템플릿)
is_scalar_type (C++26) 반영된 타입이 스칼라 타입인지 확인해요 (함수)

더 알아보기 (Learn more)

cppreference