Let's look at this snippet and suppose a, b, c and d are non-empty strings.
std::string a, b, c, d; d = a + b + c;
When calculating the sum of these 3 std::string instances, the standard library implementations create the first temporary std::string object, copy the concatenated buffers a and b in the internal buffer, then perform the same operations between the temporary string and c .
One programmer emphasized that instead of this behavior, operator+(std::string, std::string) can be defined to return a std::string_helper .
These objects are very important in order to delay the actual concatenation until the moment when it was abandoned in std::string . Obviously, operator+(std::string_helper, std::string) will be defined to return the same helper, which will "mean" the fact that it has additional concatenation to execute.
This behavior will save CPU costs for creating n-1 temporary objects, allocate their buffer, copy them, etc. So my question is: why doesn't it work like that? I cannot think of any shortcomings or limitations.
source share