I just watched cppcon talk about Bloomberg datum, variant type that uses redundancy in IEEE754 format to encode what type is stored in datum.
So I was wondering does C++ standard allow for implementations to implement std::optional more efficiently by using the same trick.
Note that this would require that sometimes binary representation of a double stored in optional does not match the binary representation of double passed to constructor.
notes: I care about standard allowing this or not, I know most/all implementations will not bother.
I know IEEE754 is not mandated by the standard, but it is allowed and checkable by implementation.
The standard requires that, if you store a value in an std::optional
, then the value must be able to be retrieved exactly as stored. Furthermore, if an optional<T>
is engaged, you can store any T
in the optional
's value without letting the optional
know you're doing it. Like this:
optional<T> opt = T{};
auto &&val = *opt;
val = <insert value here>; //opt has no idea it has been set.
Because of this, the only valid way optional<T>
can be optimized to use certain values of T
to mean that the optional
is unengaged is if it is impossible for a user to create a T
with those values. IEEE-754 implementations of double
can assume any bitpattern, and all of them are legal (even signaling NaN).
The reason other optional types can do this is because they have an implicit agreement with the user that they will not set it to certain values. std::optional<T>
has no such agreement; any value which T
can assume can be stored and retrieved.
Now, if optional<T>::operator*
and optional<T>::value
returned some kind of proxy object rather than a direct reference to T
, then this might be possible, since the proxy could handle appropriate conversions. But even then, the standard would have to specifically state that attempting to set it to one of these values will cause the value to assume an equivalent but different object representation.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With