SafeNumbers: Safer fundamental numeric types in C++20

Matt Borland

Description

SafeNumbers strives to enhance the type safety of fundamental numeric types, with an acceptable performance degradation.

It provides safe unsigned and signed integer types (u8 through u128 and i8 through i128), safe floating-point types (f32 and f64), and compile-time range-checked bounded variants (bounded_uint, bounded_int, and bounded_float).

The library is header-only, minimal dependencies (Boost.Assert, Config, and Throw_Exception), and requires only C++20.

Use Cases

Safety critical applications

Opt-In Compile-Time Checks

On optimized GCC and Clang builds the library can promote provable, compile-time-constant precondition violations, for example division by a literal zero or a shift past the width of the type, from runtime failures to build errors, at no runtime cost. This is disabled by default. See Compile-Time Precondition Checks for what is diagnosed and how to enable it with B2 and CMake.

Supported Compilers

Boost.SafeNumbers is tested natively on Ubuntu (x86_64, x86_32, s390x, aarch64, ARM32v7), macOS (x86_64, and Apple Silicon), and Windows (x86_64, x86_32, and ARM64); as well as emulated PPC64LE using QEMU with the following compilers:

  • GCC 11 and later

  • Clang 13 and later

  • Visual Studio 2022 (14.3) and later

  • Intel OneAPI DPC++ 2024.2 and later

Tested on Github Actions and Drone. Coverage can be found on Codecov.