iceoryx icon indicating copy to clipboard operation
iceoryx copied to clipboard

Optimize cxx::optional for references or create a cxx::optional_ref

Open elBoberido opened this issue 3 years ago • 0 comments

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/

elBoberido avatar Aug 13 '20 11:08 elBoberido