1

I was implementing some code that required something like this

const auto concat = std::accumulate(ints.begin(), ints.end(), string{}, 
[](string& acc, const int& val) { return string(std::move(acc))+to_string(val);});

2 questions:
1) is it safe to move from acc?
2) is it faster(than having const string& acc argument?

NoSenseEtAl
  • 28,205
  • 28
  • 128
  • 277
  • 1
    Even this should be safe, I believe: `[](string& acc, int val) -> string& { acc += to_string(val); return acc; }` No copies or moves at all (well, there's self-assignment, which may or may not be optimized). **[accumulate]/1** says that `std::accumulate` must update the accumulator by executing `acc = binary_op(acc, *i)`, and doesn't say that `binary_op` must not internally modify `acc` (which means that, yes, it should also be safe to move from `acc`). – Igor Tandetnik Sep 29 '15 at 13:59
  • @IgorTandetnik flagged for not-a-comment. (joking, but why not answer?) – Yakk - Adam Nevraumont Sep 29 '15 at 14:27
  • accepting comment :) – NoSenseEtAl Sep 29 '15 at 16:08

0 Answers0