The string concatenation operator only operates on two items at a time, either stringA + stringB, or string + string literal, or string literal + string. The result of each of these operations is the concatenated string. The operation is left-associative which means it is evaluated from left to right.
On line 3 the right hand side is evaluated first as:
( hello + ", world" ) + "!";
First, evaluate hello + ", world" which yields a string, then concatenate that string with "!". At every stage at least one of the two operands is a string so it works.
On line 7, the first two to be evaluated would be two string literals, but there is no concatenation operator defined for two string literals so it gives an error message. As you pointed out, it isn't necessary as you can just write:
const string mesg = "Hello, world" + exclam;
One other note. It is possible to "concatenate" literal strings by putting whitespace between them (not the + sign). This can be useful if you have very long literal strings. For example,
1 2 3
|
string mesg = "Four score and seven years ago,"
"our forefathers brought forth on this continent"
"a new nation, conceived in liberty and ...";
|
Note that there are no new line characters in the string (its just one line). Again the only thing that can come between the literal strings is whitespace (spaces, tabs, newlines).