• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1.. title:: clang-tidy - performance-inefficient-string-concatenation
2
3performance-inefficient-string-concatenation
4============================================
5
6This check warns about the performance overhead arising from concatenating
7strings using the ``operator+``, for instance:
8
9.. code-block:: c++
10
11    std::string a("Foo"), b("Bar");
12    a = a + b;
13
14Instead of this structure you should use ``operator+=`` or ``std::string``'s
15(``std::basic_string``) class member function ``append()``. For instance:
16
17.. code-block:: c++
18
19   std::string a("Foo"), b("Baz");
20   for (int i = 0; i < 20000; ++i) {
21       a = a + "Bar" + b;
22   }
23
24Could be rewritten in a greatly more efficient way like:
25
26.. code-block:: c++
27
28   std::string a("Foo"), b("Baz");
29   for (int i = 0; i < 20000; ++i) {
30       a.append("Bar").append(b);
31   }
32
33And this can be rewritten too:
34
35.. code-block:: c++
36
37   void f(const std::string&) {}
38   std::string a("Foo"), b("Baz");
39   void g() {
40       f(a + "Bar" + b);
41   }
42
43In a slightly more efficient way like:
44
45.. code-block:: c++
46
47   void f(const std::string&) {}
48   std::string a("Foo"), b("Baz");
49   void g() {
50       f(std::string(a).append("Bar").append(b));
51   }
52
53Options
54-------
55
56.. option:: StrictMode
57
58   When `false`, the check will only check the string usage in ``while``, ``for``
59   and ``for-range`` statements. Default is `false`.
60