vector<string> v(10, "foo"); string concat = accumulate(v.begin(), v.end(), string(""));
This example is just bad programming in any C ++ standard. This is equivalent to this:
string tmp; tmp = tmp + "foo"; //copy tmp, append "foo", then copy the result back into tmp tmp = tmp + "foo"; //copy tmp, append "foo", then copy the result back into tmp tmp = tmp + "foo"; //copy tmp, append "foo", then copy the result back into tmp tmp = tmp + "foo"; //copy tmp, append "foo", then copy the result back into tmp tmp = tmp + "foo"; //copy tmp, append "foo", then copy the result back into tmp tmp = tmp + "foo"; //copy tmp, append "foo", then copy the result back into tmp tmp = tmp + "foo"; //copy tmp, append "foo", then copy the result back into tmp tmp = tmp + "foo"; //copy tmp, append "foo", then copy the result back into tmp tmp = tmp + "foo"; //copy tmp, append "foo", then copy the result back into tmp tmp = tmp + "foo"; //copy tmp, append "foo", then copy the result back into tmp
C ++ 11 move semantics will only take care of βcopying the result back to tmpβ part of the equation. The original copies from tmp will still be copies. This is the classic Schlemiel the Painter algorithm, but even worse than the usual strcat in C.
If accumulate simply used += instead of + and = , then he would have avoided all of these copies.
But C ++ 11 gives us a way to do better by staying concise using the lambda function:
string concat; for_each(v.begin(), v.end(), [&](const string &s){ concat += s; });
EDIT: I suppose a standard library developer could implement accumulate by moving the operand to + , so tmp = tmp + "foo" will become tmp = move(tmp) + "foo" , and this will pretty much solve this problem. I am not sure that such an implementation will be strictly consistent. Currently, neither GCC, nor MSVC, nor LLVM do this. And since accumulate defined in <numeric> , we can assume that it is intended only for use with numeric types.