Surprised to hear that you want to return a reference so frequently.
Surprised to hear that you want to return a reference so frequently.
Me? References get returned all the time because you want to access some state store's vector of things without copying the vector just to ask "are any of the elements X?" or adding a new method for every `std::algorithm` method for each member you might want to use it on.
The benefit of `optional<T&>` over `T*` in an API is that the former communicates "you have write access to this thing which may not exist" whereas the second needs documentation for whether `nullptr` is a thing and whether the caller needs to `delete` it (or was it `delete[]` this time?).
This is an uncharitable characterisation of what I said.
> References get returned all the time because you want to access some state store's vector of things without copying the vector just to ask "are any of the elements X?"
This is what const references are for. Returning an optional<vector<T>&> to query if it contains an element would not be appropriate.
This to me is the clearest example of something that’s safe in Rust, and impossible to make safe in C++.
Are you asking in a theoretical world where it isn’t defined to already return a `T&`?
My overall advice here would be that if you find yourself implementing vector-like data structures very often, then it is time to take a second look the design.