Looking at a piece of code like this (added comments):
std::string some_var;
std::string some_func();
...
return "some text " + some_var + "c" + some_func();
I was wondering in what cases operator +of std::stringshould make a copy (in the sense of using copy-construction / assign, and not for copying the internal buffer, for example, if SSO is used) and what is actually copied . A quick look at cppreference was only partially useful, as it lists 12 (!) Different cases. In part, I ask you to confirm your understanding of the page:
- Case 1) makes a copy of lhs, and then copies rhs to the end of this copy.
- In C ++ 98 Case 2) - 5) the time line is constructed from an argument
char/const char*, which leads to case 1) - In C ++ 11 Case 2) - 5) the time line is constructed from
char/const char*, which then leads to case 6) or 7) - In C ++ 11 Case 6) - 12) the r-value argument will be mutated with
insert/append, and if the argument was provided char/const char*, temporary ones are not needed due to overloads on insert/append. In all cases, r is returned to facilitate further chaining. Copies are not copied (except for copies of arguments to be added / pasted at the insertion point). You may need to move the contents of the string.
Thus, a chain similar to the above example should lead to: 2) → 6) → 11) → 8), without any copies of any lhs, but simply changing the buffer of the r-value received from the first operation (creation time line).
, operator +=, operator + r-value. , operator += operator + ++ 11 , l-value?
?
: . ( ); .