Expensive use of non-member std::string operator+() instead of
a simple append
The non-member std::string operator+() function is called when
the append (or +=) method would have been more efficient
Description
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";
Risk
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.
Fix
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.
Examples
Result Information
| Group: Performance |
| Language: C++ |
| Default: Off |
Command-Line Syntax:
EXPENSIVE_STD_STRING_APPEND |
| Impact: Low |
Version History
Introduced in R2020b
See Also
Topics
- Interpret Bug Finder Results in Polyspace Desktop User Interface
- Interpret Bug Finder Results in Polyspace Access Web Interface (Polyspace Access)
- Address Results in Polyspace User Interface Through Bug Fixes or Justifications
- Address Results in Polyspace Access Through Bug Fixes or Justifications (Polyspace Access)