API Reference

Types

Unsigned Integer Types

Type Description

u8

Safe unsigned 8-bit integer

u16

Safe unsigned 16-bit integer

u32

Safe unsigned 32-bit integer

u64

Safe unsigned 64-bit integer

u128

Safe unsigned 128-bit integer

Bounded Unsigned Integer Type

Type Description

bounded_uint<Min, Max>

Safe unsigned integer constrained to a compile-time range [Min, Max]

Verified Integer Types

Type Description

verified_u8

Compile-time verified safe unsigned 8-bit integer

verified_u16

Compile-time verified safe unsigned 16-bit integer

verified_u32

Compile-time verified safe unsigned 32-bit integer

verified_u64

Compile-time verified safe unsigned 64-bit integer

verified_u128

Compile-time verified safe unsigned 128-bit integer

verified_bounded_integer<Min, Max>

Compile-time verified bounded unsigned integer constrained to [Min, Max]

Enumerations

Type Description

overflow_policy

Enum class specifying the overflow handling policy for arithmetic operations

Functions

Character Conversion

Function Description

to_chars

Converts a safe integer to a character string

from_chars

Parses a character string into a safe integer

Literals

Literal Description

_u8, _u16, _u32, _u64, _u128

User-defined literal suffixes for constructing safe integer types

Stream I/O

Function Description

operator<<, operator>>

Stream insertion and extraction for all safe integer types

Bit Manipulation

Function Description

has_single_bit

Returns true if the value is a power of two

bit_ceil

Returns the smallest power of two not less than the value

bit_floor

Returns the largest power of two not greater than the value

bit_width

Returns the number of bits needed to represent the value

rotl

Bitwise left rotation

rotr

Bitwise right rotation

countl_zero

Counts consecutive zero bits from the most significant bit

countl_one

Counts consecutive one bits from the most significant bit

countr_zero

Counts consecutive zero bits from the least significant bit

countr_one

Counts consecutive one bits from the least significant bit

popcount

Returns the number of set bits

byteswap

Reverses the bytes of the value

Integer Utilities

Function Description

isqrt

Returns the integer square root (floor of sqrt) of a safe unsigned integer

Arithmetic

Function Description

saturating_add, saturating_sub, saturating_mul, saturating_div, saturating_mod

Saturating arithmetic (clamp to min/max on overflow)

overflowing_add, overflowing_sub, overflowing_mul, overflowing_div, overflowing_mod

Overflowing arithmetic (wrap and return overflow flag)

checked_add, checked_sub, checked_mul, checked_div, checked_mod

Checked arithmetic (return std::nullopt on overflow)

wrapping_add, wrapping_sub, wrapping_mul, wrapping_div, wrapping_mod

Wrapping arithmetic (wrap silently)

strict_add, strict_sub, strict_mul, strict_div, strict_mod

Strict arithmetic (call std::exit(EXIT_FAILURE) on error)

add, sub, mul, div, mod

Generic policy-parameterized arithmetic (takes overflow_policy as template parameter)

Headers

Header Contents

<boost/safe_numbers.hpp>

Convenience header including all library types

<boost/safe_numbers/bit.hpp>

Bit manipulation functions (has_single_bit, bit_ceil, bit_floor, bit_width, rotl, rotr, countl_zero, countl_one, countr_zero, countr_one, popcount, byteswap)

<boost/safe_numbers/charconv.hpp>

Character conversion functions (to_chars, from_chars)

<boost/safe_numbers/fmt_format.hpp>

Support for library types to {fmt}. This header is not included in the convenience header since it requires external dependencies

<boost/safe_numbers/format.hpp>

Contains specializations of <format> for library types

<boost/safe_numbers/integer_utilities.hpp>

Integer utility functions (isqrt)

<boost/safe_numbers/iostream.hpp>

Stream I/O operators (operator<<, operator>>) for library types

<boost/safe_numbers/limits.hpp>

Contains specializations of <limits> for library types

<boost/safe_numbers/literals.hpp>

User-defined literal suffixes (_u8, _u16, _u32, _u64, _u128)

<boost/safe_numbers/overflow_policy.hpp>

The overflow_policy enum class

<boost/safe_numbers/unsigned_integers.hpp>

All unsigned safe integer types (u8, u16, u32, u64, u128)

<boost/safe_numbers/bounded_integers.hpp>

Bounded unsigned integer type (bounded_uint<Min, Max>)

<boost/safe_numbers/verified_integers.hpp>

Verified integer types (verified_u8, verified_u16, verified_u32, verified_u64, verified_u128, verified_bounded_integer<Min, Max>)