Why can't you add two string literals?

I know that

 
string s4 = "hello" + ", "; 


and

 
string s6 = "hello" + ", " + s2;


are illegal and do understand why

 
string s5 = s1 + ", " + "world";


is legal. My question: Why is it illegal to add two string literals? Is there something fundamentally nonsensical about the concept that I don't understand (besides that one could code it as "hello,")? Is it a technical limitation of the string class? Is it intrinsically impossible? It strikes me (as a naive C++ newbie) that it shouldn't be disallowed. If it was allowed, where is the harm?

(All code samples from Lippmann's C++ Primer)
a string literal "like this" is interpretted by the compiler as a pointer to a constant array in memory. This is why you can assign literals to char pointers:

1
2
3
const char* foo = "this works because string literals are pointers";

char bar = "You might also be surprised that this works"[2];  // bar == 'u' 


Logically, if string literals are pointers, two string literals cannot be added together for the same reason that two pointers cannot be added together:

1
2
3
4
5
6
int* ptra;
int* ptrb;

ptra + ptrb;  // can't do this -- adding pointers makes no sense

"foo" + "bar";  // same thing -- adding pointers = wtf? 


HOWEVER, C/C++ does let you simply concatenate string literals by simply leaving them adjacent to each other. Basically, just leave out the + operator:

1
2
string example = "This " "will " "be " "a" " string" ".";
cout << example;  // prints "This will be a string." 
OK, your explanation makes a lot of sense, thanks.

One followup: Isn't the "+" in

 
"foo" + "bar";


different than the "+" in

 
ptra + ptrb;


The latter "+" is the overrloaded operator of the string class -- i.e., it is concatenating two string literals indicated by two pointers. Can't the string operator "+" do the work -- it knows that they are char pointers -- why not go ahead and slap them together? Again, I'm sorry if this is just naive questions.
Isn't the "+" in

"foo" + "bar";

different than the "+" in

ptra + ptrb;

The latter "+" is the overrloaded operator of the string class


Well in my example ptra and ptrb were not strings, they were pointers (a built-in type) so there is no overloaded operator. But if they were strings, then yes, you'd be right.


Can't the string operator "+" do the work -- it knows that they are char pointers -- why not go ahead and slap them together?


You cannot overload the + operator unless one of the arguments (either the left or the right) is an instance of that class. char pointers are char pointers, they are not strings, so two char pointers does not suggest the string + operator be used. The compiler can't "figure it out" because a char pointer might be something besides a string.

You can, however, create a string temp object to get it to happen:

1
2
3
4
5
6
7
8
9
10
11
string a;

// instead of:
a = "foo" + "bar"; // error

// you could do:
a = string("foo") + "bar";

// but, again, there's not much point because you can also do either of these:
a = "foo" "bar";
a = "foobar";
Of course! The bulb finally lights! The bottom line: "You cannot overload the + operator unless one of the arguments (either the left or the right) is an instance of that class. " Brilliant. Thank you -- that was precisely what I needed to understand. I get it.
Topic archived. No new replies allowed.