I would love to have optional<T&>. Right now you have to use optional<reference_wrapper<T>>, but sadly optional<reference_wrapper<T>> is so verbose and tricky to use that T* is much easier.
Basically, optional<T&> is better than T* which is better than optional<reference_wrapper<T>>.
In order for optional<T&> to not be a poor T*, it should of course not waste space. But rather than adopting Rust’s finite list of types with “null pointer optimizations”, it would be better to have a trait based sentinel value optimization, so that custom types can have it too.
I would love to have optional<T&>. Right now you have to use
optional<reference_wrapper<T>>
, but sadlyoptional<reference_wrapper<T>>
is so verbose and tricky to use that T* is much easier.Basically,
optional<T&>
is better thanT*
which is better thanoptional<reference_wrapper<T>>
.Representation:
Hypothetical for now (as catern said), but not too late! Hint: It’s true in Rust.
Here is the thing:
optional<>
is quite wasteful, especially when you know that the underlying type has an unused value:In order for
optional<T&>
to not be a poorT*
, it should of course not waste space. But rather than adopting Rust’s finite list of types with “null pointer optimizations”, it would be better to have a trait based sentinel value optimization, so that custom types can have it too.