Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Maintainability: The Clunkyness of std::optional<std::reference_wrapper<T>> #80

Closed
ftschirpke opened this issue Dec 24, 2023 · 2 comments

Comments

@ftschirpke
Copy link
Owner

As described in #79 , using std::optional<std::reference_wrapper<T>> is great in terms of the types' descriptiveness i.e. the caller know that getting a std::optional<std::reference_wrapper<T>> means that this object might not exist and you get a reference to it.

Unfortunately, using this can be very clunky.

  • type names get very long e.g. std::optional<std::reference_wrapper<const CharacterClass>> my_class = ...;
  • the implicit conversion of std::reference_wrapper<T> to T& works in only a very few cases, thus you often need to write .value().get() because you need to unwrap the optional and the reference wrapper whereas only one calling one function would be preferable

One could implement a new type for this like e.g. OptRef<T> that simplifies the usage of these, but I wonder if obscuring the types by adding another layer of abstraction might be detrimental in the long run.

@ftschirpke
Copy link
Owner Author

I defined the aliases OptRef<T> and OptCRef<T> for std::optional<std::reference_wrapper<T>> and std::optional<std::reference_wrapper<const T>>.
While improving the experience when using these types, this doesn't solve the second issues of needing to use .value().get(). Thus, this issue remains open for now.

@ftschirpke
Copy link
Owner Author

After using these aliases for a while, I think they are a good solution while not adding too many layers of abstractions to the code.
So I am closing this issue for now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant