|
|
|
|
|
by mathstuf
769 days ago
|
|
These kinds of discussions remind me that not everyone codes in the same domain where the same patterns dominate. I think everyone would do well to avoid "but I don't need it, so it seems unnecessary" kinds of arguments and instead have the imagination that others may code in different domains where different patterns dominate. 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.