1

I was curious if the compound assignment operators are valid for multiple parameters. My guess is += will have no side-effect but may not be the same case with "-=".

std::string a; 
a += "Hello" + " "+"World"; // doesn't compile
std::string name = "Old";
a += "Hello" + name +"World"; // compiles
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
ash007
  • 31
  • 6
  • Why didn't you just try to compile it to determine if it is valid? – Jesper Juhl May 08 '19 at 15:50
  • 2
    @JesperJuhl To be fair, it's not because code compiles that it is valid. – François Andrieux May 08 '19 at 15:50
  • 2
    @FrançoisAndrieux Of course not (as I know you know I know). But it would have let OP add the additional info: "it compiles" or "it doesn't compile with this error: ..." . And doing some basic testing yourself (like "does this even compile?") is a good habit to get into. – Jesper Juhl May 08 '19 at 15:53
  • They (not me) probably thought you did not do enough research before asking the question. That is a bit subjective. – drescherjm May 08 '19 at 18:58

3 Answers3

4

This is not a valid expression because there is no operator + for string literals

"Hello" + " "+"World

(more precisely for pointers because in expressions string literals with rare exceptions are converted to pointers to their first symbols.)

Instead you could write

std::string a; 
( ( a += "Hello" ) += " " ) += "World";

But it would be more readable if to write

a += "Hello";
a += " ";
a += "World";

Or as @Bathsheba pointed out in a (valuable) comment to my answer you could use a user-defined string literal the following way

#include <string>
#include <iostream>

int main()
{
    using namespace std::string_literals;
    std::string a; 
    a += "Hello"s + " " + "World";

    std::cout << a << '\n';
}

As for this statement

a += "Hello" + name +"World";

then it can be rewriten using the operators defined for the class std::basic_string

template<class charT, class traits, class Allocator>
basic_string<charT, traits, Allocator>
operator+(const charT* lhs, const basic_string<charT, traits, Allocator>& rhs);

and

template<class charT, class traits, class Allocator>
basic_string<charT, traits, Allocator>
operator+(basic_string<charT, traits, Allocator>&& lhs, const charT* rhs);

like

a += operator +( operator +( "Hello", name ), "World" ); 

For example

#include <string>
#include <iostream>

int main()
{
    std::string a;
    std::string name = "Old";

    a += operator +( operator +( "Hello ", name ), " World" ); 

    std::cout << a << '\n';
}

Take into account that each operator returns an object of the type std::basic_string for which the operator + is defined. That is in each call of the operators there is present an object of the type std::basic_string as an argument.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
4

The compound assignment operator += doesn't come into it.

You can use += with std::string because it defines the relevant overload, but it only gets one argument.

And you've tried to pass, as that argument, an expression constructed by +ing multiple string literals. This will not compile.

Some alternatives, in this toy case, which may inspire a solution for whatever is your real case:

Option 1

// Multiple calls to +=
std::string a;
a += "Hello";
a += " ";
a += "World";

Option 2

// String literal concatenation!
std::string a; 
a += "Hello" " " "World";

Option 3

// Repeated std::string concat
std::string a;
a += std::string("Hello") + " " + "World";

Option 4

// Same
using namespace std::string_literals;
std::string a; 
a += "Hello"s + " " + "World";

Option 5

// Don't bother
std::string a;
a += "Hello World";
Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
2

The expression a += "Hello" + " " + "World" is grouped as a += ("Hello" + " " + "World").

The right hand side is a set of 3 const char[] literals. These decay to const char* pointers when applied to binary addition. Since you can't add pointers, the compiler is required to issue a diagnostic.

(Note that the ostensibly equivalent expression a = a + "Hello" + " " + "World" is compilable.)

Bathsheba
  • 231,907
  • 34
  • 361
  • 483