2

Every now and then, I see something like this in my product's code base:

void ProcessDirectories(const std::vector<std::string>& paths)
{
    for (auto&& path : paths)
    {
        // Do something with path
    }
}

I would have expected this line instead:

    for (auto& path : paths)

Where path is a normal reference to the item in the collection being enumerated.

But what does this the "double ampersand thing" imply in the enumeration

    for (auto&& path : paths)

I understand this to be called access by forwarding reference But what does it provide for a collection of strings? I understand the basics of R-values and move semantics, but I don't admit expertise here.

Or perhaps a more general question if a collection of strings is uninteresting for the different type of reference enumerations: When is it appropriate to enumerate over a collection by forwarding reference?

selbie
  • 82,148
  • 13
  • 83
  • 154
  • I would usually use `const auto&`. – Jesper Juhl May 18 '19 at 18:40
  • 1
    Fwiw, [Howard talked about this *here*](https://stackoverflow.com/questions/13130708/what-is-the-advantage-of-using-universal-references-in-range-based-for-loops). I concur with Jesper, though I maintain my `const` on the other side of type just as a matter of form (`auto const &`), Potato...uh. Potato. – WhozCraig May 18 '19 at 18:43

0 Answers0