Subject: Re: [boost] [optional] Suggestion for adding an optional_number class
From: Sergey Mitsyn (svm_at_[hidden])
Date: 2017-07-25 13:46:22
On 21.07.2017 10:47, Viktor Sehr via Boost wrote:
> I'd like to make a suggestion for adding an optional class for number
> types, called optional_number, where the max value of the number type is
> utilized as the uninitalized value.
> Differences compared to std::optional<int, float, enum etc>:
> *- No size overhead from the wrapped type*
> - Theoretically faster (althrough in practice probably negilble)
> - Can be assigned to any value except the max value
> - Operator-> is removed as it does not make sense for an integer
> - Throws if assigned the numeric_limits<T>::max
> - Also works with enums, using the underlying_type
> - The operator bool conversion is missing in the prototype implementation
> Attaching a prototype implementation (currently for C++14).
> Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
FYI there was a proposal recently for optional with the same purpose;
unfortunately, it went nowhere:
Though I think that a most valuable rationale for it is this special
optional being a tool for wrapping legacy code or systems that already
utilize special value idiom.
Also, throw on special value assignment is insane - it might be
perfectly fine to pass special values to indicate something other than
error - say, in a system it might be defined as a missing value (e.g.
NaN meaning n/a). Or a completely different thing - special value as a
non-error, everything else as an error; it may add usability to thin
wrappers to legacy API.