vendredi 31 juillet 2020

Assigning a value returned by copy to a reference

In a codebase that I am working on I am seeing a lot of things like:

std::map<int,int> m;
// ...some code...
const auto& p = m.find(10);

with reference in the type of p. The map's find returns an iterator and not a reference to one, so what is the point of writing such code and how does it work - does it call an iterators copy ctor on the returned value, as it can not just reference the temporary that is being returned, right?

This is just one example, while there are many similar cases throughout that codebase when the functions are returning by value but the caller uses

type& name = func_call()

is there some benefit to doing this that I am missing?

Aucun commentaire:

Enregistrer un commentaire