c++ accumulate with move instead of copy [migrated]
- by user74399
I have the following code
auto adder = [](string& s1, const string& s2)->string&&
{
if (!s1.empty())
s1 += " ";
s1 += s2;
return move(s1);
};
string test;
test.reserve(wordArray.size() * 10);
string words = accumulate(wordArray.begin(), wordArray.end(), move(test), adder);
What I would like here is to avoid string copying. Unfortunately this is not accomplished by the vs2012 implementation of accumulate. Internally accumulate calls another function _Accumulate and the rvalue functionality gets lost in the process.
It I instead call the _Accumulate function like so
string words = Accumulate(wordArray.begin(), wordArray.end(), move(test), adder);
I get the intended performance gain.
Must the std library be rewritten to take rvalue arguments into consideration?
Is there some other way I may use accumulate to accomplish what I want without cheating to much?