Hello!
I am asking for some feedback if my library idea is good enough to be part
of boost if it will meet the requirements.
When i started this small utility lib i had 3 goals in my mind:
- Write cleaner interfaces
- Be safer with pre and post conditions
- Eliminate redundant checks whenever possible at compile time
The goal is to solve this problem for primitive types.
*Example "bad" code:*
void set_red_color(int red) {
//Why the precondition is not visible on the interface?
assert(red >= 0 && red <= 255);
}
*Example "good" code:*
void set_red_color(bounded_i<0, 255> red) {}
*Example "bad" code:*
void foo(int bar) {
assert(bar >= 0);
}
*Example "good" code:*
void foo(positive_i bar) {}
or
void foo(lower_bounded_i<0> bar) {}
Basically the main idea is to have an invariant_host class, which is
customizable via policies:
-* Invariant policy:* Define a static check() function which ensure the
invariant.
- *Fail policy: *Define a static trigger_assert function which is called
when the invariant policy::check is failed.
If the fail policy is disabled (constexpr bool flag), then the checks
doesn't happen.
Defining your own class looks like this:
template<typename PrimitiveType>
using my_type = invariant_host