A Developer's Guide to std::make_unsigned in C++

2025-09-15

At its core, std::make_unsigned is a type trait in C++. It's a template that takes a type T and, at compile-time, determines the corresponding unsigned integer type. For example, std::make_unsigned<int>::type would be unsigned int.

This is incredibly useful in metaprogramming because it allows you to write generic code that can work with both signed and unsigned integer types without having to write separate overloaded functions or templates.

While std::make_unsigned is straightforward, a few common issues can trip you up.

Non-Integer Types
The most common mistake is trying to use std::make_unsigned on a type that isn't an integer. This includes floating-point types like float or double, and even bool. When you do this, you get a compilation error, as the standard doesn't define a corresponding unsigned type for them.

Aliasing with long long
On some platforms, long and long long can be the same size. If you're not careful, this can lead to unexpected behavior if your code relies on a specific size difference.

Loss of Information
This isn't a bug, but a key consideration. If you convert a negative signed integer to its unsigned counterpart, you lose the sign information. For example, a signed short with the value -1 will become a large positive unsigned short. This is due to how two's complement arithmetic works.

Here are a couple of ways you can handle these situations and write more robust code.

A great way to prevent the "non-integer type" problem is to use a static_assert. This will cause the program to fail to compile with a clear error message if the input type isn't an integer, making debugging much easier.

#include <type_traits>
#include <iostream>

template <typename T>
void do_something_with_unsigned_integer(T value) {
    // Check if T is an integer type at compile time
    static_assert(std::is_integral<T>::value, "Error: T must be an integral type!");

    // Use std::make_unsigned to get the unsigned version
    using UnsignedType = typename std::make_unsigned<T>::type;

    UnsignedType unsigned_value = value;
    std::cout << "Original value: " << value << std::endl;
    std::cout << "Unsigned value: " << unsigned_value << std::endl;
}

int main() {
    do_something_with_unsigned_integer(100);
    do_something_with_unsigned_integer(-5);
    // The following line would cause a compile-time error!
    // do_something_with_unsigned_integer(3.14); 

    return 0;
}

Sometimes you might want to provide a fallback type if the input isn't an integer. This is less common but can be useful in highly specialized template libraries. std::conditional allows you to choose one of two types based on a compile-time condition.

#include <type_traits>
#include <iostream>

template <typename T>
void process_with_safe_unsigned_type(T value) {
    // If T is an integral type, use its unsigned version. Otherwise, use a default type like 'size_t'.
    using ProcessedType = typename std::conditional<
        std::is_integral<T>::value,
        typename std::make_unsigned<T>::type,
        size_t
    >::type;

    ProcessedType processed_value = (ProcessedType)value;
    std::cout << "Processed value: " << processed_value << std::endl;
}

int main() {
    process_with_safe_unsigned_type(42);
    process_with_safe_unsigned_type(-10);
    process_with_safe_unsigned_type(3.14); // This will treat 3.14 as size_t, which is usually unsigned long long
    
    return 0;
}

In the second example, passing 3.14 will convert it to a size_t (an unsigned integer type) without a compilation error, but you still get a compiler warning about the loss of data. This approach is more about avoiding a hard stop at compile time and is a bit more advanced.


cpp



C++ Ranges: Understanding and Working with Iterators

The chunk_view::outer-iterator is an input or forward iterator, meaning it only supports operator++ for moving to the next chunk


Beyond chunk_by_view: C++ Alternatives for Grouping Data

First, let's understand what chunk_by_view does. It takes a range and a predicate (a function that returns true or false). It then groups consecutive elements as long as the predicate returns true for a pair of adjacent elements


Mastering C++ Iterators: A Guide to std::basic_const_iterator::base() and Its Alternatives

When you're working with std::basic_const_iterator::base, you might run into a few common issues. Let's look at them and how to handle them



Mastering C++ std::chrono::year_month_day_last Formatting

When using std::formatter with std::chrono::year_month_day_last, you're essentially trying to format a date that's defined as the last day of a specific month in a given year


Optimizing C++ Unordered_Map Performance with max_load_factor

std::unordered_map is an associative container that stores elements in buckets. The load factor is a measure of how full the hash table is


Mastering std::string_view::back() in C++

First, let's quickly review what std::basic_string_view::back() is. It's a method that returns a reference to the last character of a std::string_view


Defaulting Special Member Functions in C++

The default keyword in C++ is a powerful tool for a specific purpose telling the compiler to automatically generate a special member function for you


C++ std::timed_mutex::lock Pitfalls and Alternatives

First, let's quickly recap what std::timed_mutex::lock does. It's part of the C++ standard library's concurrency support


How to Properly Handle set_value_at_thread_exit in C++ Multithreading

Unlike set_value, which sets the value right away, set_value_at_thread_exit is deferred. The value is only set when the thread finishes


C++ std::placeholders: Common Issues and Modern Alternatives

std::placeholders are a part of the C++ Standard Library used to create binders, which essentially pre-fill function arguments