23

Is there any noticeable difference between the two lines? My coworker says that using += is "faster" but I don't see why they should be any different:

string s1 = "hello";
string s2 = " world";

// Option 1
s1 += s2;

// Option 2
s1.append(s2);

To clarify, I am not asking about the usage differences between the two functions - I am aware that append() can be used for a wider variety of uses and that operator += is somewhat more specialized. What I care about is how this particular example gets treated.

ClydeTheGhost
  • 1,473
  • 2
  • 17
  • 31
  • 3
    ask you coworker for a test app that shows the difference. Its certainly faster to type :-) – pm100 Jul 12 '17 at 20:49
  • 2
    Possible duplicate of [Why is std::string::append() less powerful than std::string::operator+()?](https://stackoverflow.com/questions/35646537/why-is-stdstringappend-less-powerful-than-stdstringoperator) – Erik Godard Jul 12 '17 at 20:50
  • 1
    I would expect both versions to have the exact same result using any good standard library with any good compiler. However, [`append` has no restrictions on its complexity](http://en.cppreference.com/w/cpp/string/basic_string/append#Complexity), while [`operator+=` does](http://en.cppreference.com/w/cpp/string/basic_string/operator%2B%3D#Complexity). – Justin Jul 12 '17 at 20:51
  • @ErikGodard I meant other than the obvious semantic differences, i.e. append can take different kinds of arguments. For this specific example, is there any difference between using one or the other form. – ClydeTheGhost Jul 12 '17 at 20:53
  • I would probably favor using `+=` simply because its meaning is obvious in analogy with `+`, and `+` does not have an alternative afaik. Another nice bonus of `+=` and `+` is that you can write them yourself if you have a custom type you want to efficiently append to strings; operators get considered as both free and member functions in most cases, `append` has to be a member. In short perf is not a good argument but use `+=` anyhow. – Nir Friedman Jul 12 '17 at 20:57
  • 1
    If I see someone using `append(str)` this immediately raises eyebrows as I have to ask myself why they are not using `operator+=`. It could be a mistake, maybe they intended to call an overload of `append()` that provides different functionality than `operator+=`. When using `operator+=` the intention is clearer in my opinion. – zett42 Jul 12 '17 at 21:08
  • 1
    @Justin neither `append` nor `operator+=` have any complexity restrictions. (specially cause the operator was initially designed in terms of append) – ABaumstumpf Sep 05 '22 at 18:30

2 Answers2

23

According to the standard concerning string::op+= / online c++ standard draft, I wouldn't expect any difference:

basic_string& operator+=(const basic_string& str);

(1) Effects: Calls append(str).

(2) Returns: *this.

Stephan Lechner
  • 34,891
  • 4
  • 35
  • 58
  • Seems legit but what is the source of this documentation? – ClydeTheGhost Jul 12 '17 at 21:08
  • @AdrianPadin Well, he linked it. – zett42 Jul 12 '17 at 21:10
  • @Adrian Padin: Its an online draft version ("C++ standard working draft N3337 (C++11 + editorial fixes)", which is available for free and therefore often used here on SO. The normative version will very likely not differ in this point, but it is not free and cannot be linked therefore. – Stephan Lechner Jul 12 '17 at 21:14
14

In Microsoft STL implementation, the operator += is an inline function, which calls append(). Here are the implementations,

  • string (1): string& operator+= (const string& str)
basic_string& operator+=(const basic_string& _Right) {
    return append(_Right);
}
  • c-string (2): string& operator+= (const char* s)
basic_string& operator+=(_In_z_ const _Elem* const _Ptr) {
    return append(_Ptr);
}
  • character (3): string& operator+= (char c)
basic_string& operator+=(_Elem _Ch) {
    push_back(_Ch);
    return *this;
}
Biswapriyo
  • 3,491
  • 3
  • 22
  • 42
PatrickF
  • 594
  • 2
  • 11