On 24/07/2017 19:29, Viktor Sehr wrote:
I expressed myself badly perhaps, what I mean is that users cannot utilize the max value in any way. It's hidden inside the optional, meaning that any handling of the max value is undefined or throws. Perhaps even a static max member function returning (numeric_limits::max<T>() -1) should be added.
Memory is cheap; the extra tests required to throw on: optional_number<unsigned> x = 0; --x; (Or the developer headaches that would ensue if made UB and arithmetic operations occasionally make the value pop in and out of existence or do other weird things.) Are less cheap. If you're worried about wasting memory in large arrays of numbers, consider a hybrid approach where you have an array of regular integers and a bitset that separately indicates validity. This might use an extra cache line but still should perform reasonably well.