mercredi 27 juillet 2016

const auto& for storing functions results, is it worthwhile?

Let assume that we have a function that returns a complex object like std::string:

std::string find_path(const std::string& filename);

Is it worthwhile to store the result of calling that method in the const auto&?

void doSth() {
   //...
   const auto& path = find_path(filename);
   //...
} 

That kind of approach prevents copying/moving the object. So it is good. But on the other hand, auto has been introduced to unify the left side of assignation. Herb Sutter in his presentation from CppCon2014 mentions about C++ left-to-right modern style https://www.youtube.com/watch?v=xnqTKD8uD64 (39:00-45:00).

In C++98 storing the std::string at const ref was fine. How is it in C++11?

Aucun commentaire:

Enregistrer un commentaire