Namespaces
Variants
Views
Actions

std::make_unsigned

From cppreference.com
< cpp‎ | types
Revision as of 05:22, 24 January 2021 by D41D8CD98F (Talk | contribs)

 
 
Utilities library
General utilities
Relational operators (deprecated in C++20)
 
 
Defined in header <type_traits>
template< class T >
struct make_unsigned;
(since C++11)

If T is an integral (except bool) or enumeration type, provides the member typedef type which is the unsigned integer type corresponding to T, with the same cv-qualifiers.

If T is signed or unsigned char, short, int, long, long long; the unsigned type from this list corresponding to T is provided.

If T is an enumeration type or char, wchar_t, char8_t(since C++20), char16_t, char32_t; the unsigned integer type with the smallest rank having the same sizeof as T is provided.

Otherwise, the behavior is undefined.

(until C++20)

Otherwise, the program is ill-formed.

(since C++20)

If the program adds specializations for std::make_unsigned, the behavior is undefined.

Contents

Member types

Name Definition
type the unsigned integer type corresponding to T

Helper types

template< class T >
using make_unsigned_t = typename make_unsigned<T>::type;
(since C++14)

Example

#include <iostream>
#include <type_traits>
 
int main() {
    typedef std::make_unsigned<char>::type char_type;
    typedef std::make_unsigned<int>::type int_type;
    typedef std::make_unsigned<volatile long>::type long_type;
 
    bool ok1 = std::is_same<char_type, unsigned char>::value; 
    bool ok2 = std::is_same<int_type, unsigned int>::value;
    bool ok3 = std::is_same<long_type, volatile unsigned long>::value;
 
    std::cout << std::boolalpha
    << "char_type is 'unsigned char'?          : " << ok1 << '\n'
    << "int_type  is 'unsigned int'?           : " << ok2 << '\n'
    << "long_type is 'volatile unsigned long'? : " << ok3 << '\n';
}

Output:

char_type is 'unsigned char'?          : true
int_type  is 'unsigned int'?           : true
long_type is 'volatile unsigned long'? : true

See also

(C++11)
checks if a type is a signed arithmetic type
(class template) [edit]
checks if a type is an unsigned arithmetic type
(class template) [edit]
obtains the corresponding signed type for the given integral type
(class template) [edit]