std::string operator+() instead of
a simple appendThe non-member std::string operator+() function is called when
the append (or +=) method would have been more efficient
This defect occurs when you append to a string using the non-member function
std::string operator+(), for
instance:
std::string s1; s1 = s1 + "Other";
The operation:
s1 = s1 + "Other";
std::string operator+() for the string concatenation on the
right-hand side of the assignment. The function returns a temporary string, which is then
assigned to s1.Directly calling the member function operator+=() avoids the creation
of this temporary string and is more efficient.
To append to a string, use the member function operator+=(), for
instance:
std::string s1; s1 += "Other";
append, for
instance:std::string s1;
s1.append("Other");Performance improvements might vary based on the compiler, library implementation, and environment that you are using.
| Group: Performance |
| Language: C++ |
| Default: Off |
Command-Line Syntax:
EXPENSIVE_STD_STRING_APPEND |
| Impact: Low |