1. 9
    1. 4

      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>>.

    2. 3


      Not only do T* and optional<T&> have the same representation, but they even have the same meaning

      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:

      #include <optional>
      struct RefWrapper {
      	int& ref;
      enum class NonNullvaluedEnum {
      	one = 1,
      static_assert(sizeof(std::optional<int*>) == 2 * sizeof(int*));
      static_assert(sizeof(std::optional<RefWrapper>) == 2 * sizeof(RefWrapper));
      static_assert(sizeof(std::optional<NonNullvaluedEnum>) == 2 * sizeof(int));

      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.