Namespaces
Variants

std:: is_signed

From cppreference.net
Metaprogramming library
Type traits
Type categories
(C++11)
(C++11) ( DR* )
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)
is_signed
(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_signed ;
(C++11부터)

std::is_signed UnaryTypeTrait 입니다.

T 가 부호 있는 산술 타입인지 확인합니다.

  • 만약 std:: is_arithmetic < T > :: value true 인 경우, 멤버 상수 value T ( - 1 ) < T ( 0 ) 값으로 제공합니다.
  • 그렇지 않은 경우, 멤버 상수 value false 값으로 제공합니다.

프로그램이 std::is_signed 또는 std::is_signed_v 에 대한 특수화를 추가하는 경우, 그 동작은 정의되지 않습니다.

목차

템플릿 매개변수

T - 확인할 타입

헬퍼 변수 템플릿

template < class T >
constexpr bool is_signed_v = is_signed < T > :: value ;
(C++17부터)

std:: integral_constant 로부터 상속됨

멤버 상수

value
[static]
true 만약 T 가 부호 있는 산술 타입이면, false 그렇지 않으면
(public static member constant)

멤버 함수

operator bool
객체를 bool 로 변환, value 반환
(public member function)
operator()
(C++14)
value 반환
(public member function)

멤버 타입

타입 정의
value_type bool
type std:: integral_constant < bool , value >

가능한 구현

namespace detail
{
    template<typename T, bool = std::is_arithmetic<T>::value>
    struct is_signed : std::integral_constant<bool, T(-1) < T(0)> {};
    template<typename T>
    struct is_signed<T, false> : std::false_type {};
}
template<typename T>
struct is_signed : detail::is_signed<T>::type {};

예제

#include <iostream>
#include <type_traits>
class A {};
static_assert(std::is_signed_v<A> == false);
class B { int i; };
static_assert(std::is_signed_v<B> == false);
enum C : int {};
static_assert(std::is_signed_v<C> == false);
enum class D : int {};
static_assert(std::is_signed_v<D> == false);
static_assert
(
    std::is_signed<signed int>::value == true and // C++11
    std::is_signed<signed int>() == true and      // C++11
    std::is_signed<signed int>{} == true and      // C++11
    std::is_signed_v<signed int> == true and      // C++17
    std::is_signed_v<unsigned int> == false and
    std::is_signed_v<float> == true and
    std::is_signed_v<bool> == false and
    std::is_signed_v<signed char> == true and
    std::is_signed_v<unsigned char> == false
);
int main()
{
    // char의 부호 여부는 구현에 따라 정의됨:
    std::cout << std::boolalpha << std::is_signed_v<char> << '\n';
}

가능한 출력:

true

결함 보고서

다음의 동작 변경 결함 보고서들은 이전에 발표된 C++ 표준에 소급 적용되었습니다.

DR 적용 대상 게시된 동작 올바른 동작
LWG 2197 C++11 value 값이 T 가 산술 타입이 아닌 경우에도 true 일 수 있음 이 경우에는 false 만 가능함

참고 항목

타입이 부호 없는 산술 타입인지 검사합니다
(클래스 템플릿)
[static]
부호 있는 타입을 식별합니다
( std::numeric_limits<T> 의 public static 멤버 상수)
타입이 산술 타입인지 검사합니다
(클래스 템플릿)
주어진 정수 타입에 해당하는 부호 있는 타입을 얻습니다
(클래스 템플릿)
주어진 정수 타입에 해당하는 부호 없는 타입을 얻습니다
(클래스 템플릿)