intel / safe-arithmetic

Safe arithmetic library for C++20 and above. Safe arithmetic ensures correctness of arithmetic operations at compile-time. It protects against overflow, underflow, divide by zero, and out-of-bounds index access. This provides both functional correctness as well as greater protection against related security threats.
https://intel.github.io/safe-arithmetic/
Boost Software License 1.0
83 stars 10 forks source link

🧑‍🔬 Make sure safe arithmetic works with a dimensional analysis library (like mpusz/units) #8

Open lukevalenty opened 1 year ago

lukevalenty commented 1 year ago

Some notes on how this might work.

safe::var<int, ival<0, 1023>, km / h> current_speed = 100_s32 * km / h;

constexpr auto speed_limit = 100_s32 * km / h;
units::some_type<safe::var<int, ival<0, 1023>>> current_speed = 100_s32 * km / h;

safe::var<unit::some_type<int, km / h>, ival<0, 1023>> current_speed = 100_s32 * km / h;
units::some_type<safe::var<int, ival<100, 100>>>
lukevalenty commented 1 year ago

another variation of the top that does a better job satisfying the DRY principle:

auto current_speed = s32_ival<0, 1023>{100_s32} * km / h;