(Actually, @honk beat me to it as regards passing-by-value. To be different, I'll extend my answer to discuss using &&
explicitly)
Drop the &
, and just pass it by value instead.
You're using lambdas, therefore you're using c++11, therefore you should just pass it by value. Yes, it will be just as fast.
C++11 is very good at noticing when a temporary is being 'copied'. It will replace the copy with a move. It will then be fast, and arguably it will be easier to read.
So, even though you might know nothing about &&
and std::move
and so on, it's safe to say that you should start moving towards passing things by value instead of by reference. It will lead to more readable code, and it shouldn't lead to a slowdown if used at the right time.
Basically, it's fast if you pass a temporary value, such as that returned by a function, into a function, but only if you are using a standard container such as vector
. Otherwise, you'll need to write your own &&
-aware constructors to take advantage of the possible speed up.
Update: If you're determined to force it to be by reference, and/or you are using custom containers, then you could replace &
with &&
. This will allow you to pass a non-const reference. This is called a rvalue-reference.
As has been pointed out by many others, the temporary will be destroyed just after DoSomething
returns. Bear this in mind. It means that you will want to fully consume the contents of the array before it returns. Maybe you print it to the screen, or store it in a database. Alternatively, you might copy or move the data into another object and return that.