chromium / subspace

A concept-centered standard library for C++20, enabling safer and more reliable products and a more modern feel for C++ code.; Also home of Subdoc the code-documentation generator.
https://suslib.cc
Apache License 2.0
89 stars 15 forks source link

Add sus::num::Bounded* types #216

Open danakj opened 1 year ago

danakj commented 1 year ago

Like https://doc.rust-lang.org/std/num/index.html

They should be NeverValueField types so they don't require an extra bool in Option.

danakj commented 1 year ago

I think a more general thing here would be integral types with a fixed range. NonZeroI8 is just an i8 with range 1..=MAX. But it'd be nice to instead have BoundedI8<1, i8::MAX> or something. The compiler can optimize on the bounds then, as .get() could __assume() stuff about the returned value?

The NeverValueField would be the min_bound-1 or max_bound+1, assuming either one is not MIN/MAX.

Bounded type A should implicitly convert to Bounded type B if B has bounds which cover A, regardless of the actual storage type.

danakj commented 1 year ago

The compiler can optimize on the bounds then, as .get() could __assume() stuff about the returned value?

Yes, as long as the method that returns the inner value is inlined, then the caller will optimize based on the bounds in Clang and GCC (MSVC does poorly but can clearly be improved).

https://godbolt.org/z/dxTn8vxKM