iceoryx
iceoryx copied to clipboard
Optimize cxx::optional for references or create a cxx::optional_ref
Brief feature description
The memory consumption of cxx::optional with nullable types can be reduced by removing the m_hasValue flag and inferring that information from the stored pointer.
Detailed information
A cxx::optional<T*> or cxx::optional<T&> doesn't need the m_hasValue flag, but can infer the information from the value being a nullptr. It would be similar to a std::reference_wrapper, but with the advantage of being nullable so that instead of returning a raw pointer, a optional of a raw pointer or reference can be used. The benefit would be that when the optinoal contains a value, it would never be a nullptr and a developer can tell from the signature that the function might have a nulltopt.
Further information to consider when implementing:
- https://foonathan.net/2018/07/optional-reference/
- https://www.jonathanmueller.dev/talk/cppnow2018/
- https://www.fluentcpp.com/2018/10/02/pointers-references-optional-references-cpp/
- https://www.fluentcpp.com/2018/10/05/pros-cons-optional-references/