This question mentioned the obvious, idiomatic usage of C++11 range-based for.

for (auto& elem: container) {
  // do something with elem
}

I've been having doubts about the kind of reference you're supposed to use, though. Input iterators may return rvalues. Although the implicit type introduced by auto could be deduced to const which would bind to an rvalue, that doesn't seem to occur.

Is the best general practice to use perfect forwarding?

for (auto && elem: container) {
  // do something with elem
}

I see no downside here, but it looks a little too cute. Maybe I still just haven't written enough C++11.

Edit
Report