A C++20 implementation of safe (wrap around) integers following MISRA C++ rules

Overview

PSsimplesafeint

A C++20 implementation of safe (wrap around) integers following MISRA C++ rules.

I might also work on a C++17 implementation, but this will look uglier due to SFINAE and DIY comparison.

It provides the following types in namspace psssint and corresponding UDL operators in namespace psssint::literals:

// unsigned
enum class ui8;   auto a = 1_ui8;
enum class ui16;  auto b = 2_ui16;
enum class ui32;  auto c = 3_ui32;
enum class ui64;  auto d = 4_ui64;
// signed
enum class si8;   auto e = 5_si8;
enum class si16;  auto f = 6_si16;
enum class si32;  auto g = 7_si32;
enum class si64;  auto h = 8_si64;

Some extra features:

  • an output operator is defined
  • The UDL operators check for range.
  • not recommended features:
    • to_int(val) promotes the safe integer val to built-in integer keeping its signedness
    • to_underlying(val) allows access to the underlying-type's value, use with care in operations.
    • from_int(val) converts allowed integer type's value to corresponding safe integer type.
    • from_int_to<safeinttype>(val) converts integer to safe integer type with range check (throws).

The following MISRA C++ recommendations for integer usage are implemented:

  • No mixing of signed and unsigned types or with any standard-defined arithmetic types
  • Integral promotion (only when mixing types in operations) will keep signedness
  • Operations using the same operand type result in said type (no implicit promotion)
  • Operations using mixed types of the same signedness promote to the bigger type first
  • All operations wrap safely (2's complement), even for signed operands
  • Bitwise operations are only defined for unsigned types
  • Negation is only defined for signed types
  • Comparison is only available through operands of the same type, using built-in <=>

What else do you want?

You might also like...
C++ implementation of a fast hash map and hash set using hopscotch hashing

A C++ implementation of a fast hash map and hash set using hopscotch hashing The hopscotch-map library is a C++ implementation of a fast hash map and

C++ implementation of a fast hash map and hash set using robin hood hashing

A C++ implementation of a fast hash map and hash set using robin hood hashing The robin-map library is a C++ implementation of a fast hash map and has

C++14 header only result monad implementation

constexpr Either S, E C++14 header only result monad implementation. Features constexpr support 0 dependencies single header Status in development T

Typesafe, Generic & Fastest Set Data structure implementation in C
Typesafe, Generic & Fastest Set Data structure implementation in C

Typesafe & Fast as fuck Set in C Key Features Extremely fast non-cryptographic hash algorithm XXHash Complete Typesafe APIs Double Hashing to avoid bo

A fast Python Common substrings of multiple strings library with C++ implementation

A fast Python Common substrings of multiple strings library with C++ implementation Having a bunch of strings, can I print some substrings which appea

Implementation of various data structures and algorithms.
Implementation of various data structures and algorithms.

Data Structures and Algorithms A place where you can find and learn the copious number of algorithms in an efficient manner. This repository covers va

Custom Malloc/Free implementation in C

To run the tests simply compile using "make" and run the executable "memory" with "./memory". The tests run automatically from main(), there are 3 tes

An intrusive C++17 implementation of a Red-Black-Tree, a Weight Balanced Tree, a Dynamic Segment Tree and much more!

This is Ygg (short for Yggdrasil), a C++17 implementation of several intrusive data structures: several balanced binary search trees: a red-black Tree

Own implementation of a linked list stack in C++.

Linked-List-Stack-CPP Own implementation of a linked list stack in C++. This is my manual implementation of a linked list stack. This stack is abstrac

Comments
  • Make division by zero well defined

    Make division by zero well defined

    As of https://link.springer.com/article/10.1007/s00224-007-9035-4 divison by zero should be 0 to have well-defined total operations.

    Currently the division operations have assert(divisor != 0); as a check.

    Alternatively, the zero check could be made customizable by a macro different from assert() or throw an exception (dependency on exception handling, not good).

    Will request feedback... (some are against it).

    opened by PeterSommerlad 2
Owner
Peter Sommerlad
Peter Sommerlad is a consultant and trainer for Safe & Modern C++ and Agile Software Engineering. Peter was professor and director of @IFS-HSR
Peter Sommerlad
Given two arrays X and Y of positive integers, find the number of pairs such that x^y > y^x (raised to power of) where x is an element from X and y is an element from Y.

Number-of-pairs Given two arrays X and Y of positive integers, find the number of pairs such that x^y > y^x (raised to power of) where x is an element

Rajesh Kumar Sah 1 Nov 20, 2021
lightweight, compile-time and rust-like wrapper around the primitive numerical c++ data types

prim_wrapper header-only, fast, compile-time, rust-like wrapper around the primitive numerical c++ data types dependencies gcem - provides math functi

null 1 Oct 22, 2021
Easy to use, header only, macro generated, generic and type-safe Data Structures in C

C Macro Collections Easy to use, header only, macro generated, generic and type-safe Data Structures in C. Table of Contents Installation Contributing

Leonardo Vencovsky 347 Jan 8, 2023
Library of generic and type safe containers in pure C language (C99 or C11) for a wide collection of container (comparable to the C++ STL).

M*LIB: Generic type-safe Container Library for C language Overview M*LIB (M star lib) is a C library enabling to use generic and type safe container i

PpHd 571 Jan 5, 2023
A library of type safe sets over fixed size collections of types or values, including methods for accessing, modifying, visiting and iterating over those.

cpp_enum_set A library of type safe sets over fixed size collections of types or values, including methods for accessing, modifying, visiting and iter

Carl Dehlin 22 Jun 16, 2022
Functional, Type safe, Lazy abstractions for generic iterators in C

C-Iterplus Functional abstractions for working with iterators, as demonstrated in c-iterators. Finding the longest common prefix of an array of string

Chase 27 Oct 18, 2022
Eggs.Variant is a C++11/14/17 generic, type-safe, discriminated union.

Eggs.Variant Introduction Eggs.Variant is a C++11/14/17 generic, type-safe, discriminated union. See the documentation at http://eggs-cpp.github.io/va

null 138 Dec 3, 2022
Cross-platform STL-styled and STL-compatible library with implementing containers, ranges, iterators, type traits and other tools; actors system; type-safe config interface.

Yato A small repository where I'm gatherting useful snippets and abstractions for C++ development. Yato includes 3 main modules: multidimensional cont

Alexey 10 Dec 18, 2022
libsrt is a C library for writing fast and safe C code, faster.

libsrt is a C library for writing fast and safe C code, faster. It provides string, vector, bit set, set, map, hash set, and hash map handling. Suitable for soft and hard real-time. Allows both heap and stack allocation. *BETA* (API still can change: suggestions are welcome)

null 517 Dec 24, 2022
This repository provides implementation of an incremental k-d tree for robotic applications.

ikd-Tree ikd-Tree is an incremental k-d tree designed for robotic applications. The ikd-Tree incrementally updates a k-d tree with new coming points o

HKU-Mars-Lab 362 Jan 4, 2023