Inefficient use of basic strings

The C++ basic string package is being used inefficiently.

The C++ standard template library defines a number of string operators. Some operators, such as string concatenation (+), tend to create and destroy temporary objects. This warning message is used to point out an alternative way to compute the same result more efficiently, by reducing the number of temporaries required.

ID

Observation

Description

1

Call site

The place the inefficient operation was found.

Example

          
#include <iostream>
#include <string>

using namespace std;

int main(int argc, char **argv)
{
    string a, b(argv[1]), c(argv[2]);
    a = b + c; // better is (a = b), a += c; or (a = b) += c;
    cout << b << " + " << c << " = " << a;
    return 0;
}
        

Copyright © 2010, Intel Corporation. All rights reserved.