std::is_unsigned - cppreference.com

From cppreference.com

template<classT>structis_unsigned;(since C++11)std::is_unsigned is a

UnaryTypeTrait

.

Checks whether T is an unsigned arithmetic type.

If std::is_arithmetic<T>::value is true, provides the member constant value equal to T(0)<T(-1).

Otherwise, provides the member constant value equal to false.

If the program adds specializations for std::is_unsigned or std::is_unsigned_v, the behavior is undefined.

Template parameters

T - a type to check Helper variable template

template<classT>constexprboolis_unsigned_v=is_unsigned<T>::value;(since C++17)Inherited from

std::integral_constant

Member constants

value

[static]

true if T is an unsigned integral type, false otherwise
(public static member constant)Member functions

operator bool

converts the object to bool, returns value
(public member function)operator()

(C++14)

returns value
(public member function)Member types

Type Definition value_typebooltypestd::integral_constant<bool,value>Possible implementation

namespacedetail{template<typenameT,bool=std::is_arithmetic<T>::value>structis_unsigned:std::integral_constant<bool,T(0)<T(-1)>{};template<typenameT>structis_unsigned<T,false>:std::false_type{};}// namespace detailtemplate<typenameT>structis_unsigned:detail::is_unsigned<T>::type{};Example

Run this code

#include<iostream>#include<type_traits>classA{};static_assert(std::is_unsigned_v<A>==false);enumB:unsigned{};static_assert(std::is_unsigned_v<B>==false);enumclassC:unsigned{};static_assert(std::is_unsigned_v<C>==false);structS{unsignedp:1;intq:1;};static_assert(std::is_unsigned_v<decltype(S::p)>not_eqstd::is_unsigned_v<decltype(S::q)>);static_assert(std::is_unsigned_v<float>==false&&std::is_unsigned_v<signedint>==false&&std::is_unsigned_v<unsignedint>==true&&std::is_unsigned_v<bool>==true);intmain(){// signedness of char is implementation-defined:std::cout<<std::boolalpha<<std::is_unsigned<char>::value<<'\n';}Possible output:

false Defect reports

The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

DR Applied to Behavior as published Correct behavior

LWG 2197

C++11 value could be true even if T is not an arithmetic type can only be false in this case See also

is_signed

(C++11)

checks if a type is a signed arithmetic type
(class template)

[edit]

is_signed

[static]

identifies signed types
(public static member constant of std::numeric_limits<T>)

[edit]

is_arithmetic

(C++11)

checks if a type is an arithmetic type
(class template)

[edit]

make_signed

(C++11)

obtains the corresponding signed type for the given integral type
(class template)

[edit]

make_unsigned

(C++11)

obtains the corresponding unsigned type for the given integral type
(class template)

[edit]

is_unsigned_type

(C++26)

checks if reflection represents an unsigned arithmetic type
(function)

[edit]