135

I am reading Accelerated C++ by Koenig. He writes that "the new idea is that we can use + to concatenate a string and a string literal - or, for that matter, two strings (but not two string literals).

Fine, this makes sense I suppose. Now onto two separate exercises meant to illuminate this .

Are the following definitions valid?

const string hello = "Hello";

const string message = hello + ",world" + "!";

Now, I tried to execute the above and it worked! So I was happy.

Then I tried to do the next exercise;

const string exclam = "!";

const string message = "Hello" + ",world" + exclam;

This did not work. Now I understand it has something to do with the fact that you cannot concatenate two string literals, but I don't understand the semantic difference between why I managed to get the first example to work (isn't ",world" and "!" two string literals? Shouldn't this not have worked?) but not the second.

6
  • 4
    const string message = "Hello" ",world" + exclam (e.g. omitting the first +) shoud work just fine.
    – n0rd
    May 19, 2011 at 16:36
  • 1
    @Joe - Why would anyone write "Hello" + ", world!" when you can do "Hello, world!". As usual C++ has an awesome and simple workaround for a perceived problem. :-)
    – Bo Persson
    May 19, 2011 at 16:49
  • 2
    @Bo The only thing I can think of is if you use a definition (#define) May 19, 2011 at 16:52
  • @Joe Even then, you're more likely to write "Hello" ", world!" (without the +). There are a number of complaints one could make about C++, but I don't think it's handling here is one of them. It's exactly the same thing as if you wrote 1 / 3 + 1.5, and complained because the division was integral division. For better or for worse, this is the way most languages work. May 19, 2011 at 17:56
  • 4
    @Bo Persson Actually this feature "hello" " world" == "hello world" is useful if you have to write a long string and don't want it to go out of your window or you want to be within some line length constriction. Or if one of the strings is defined in a macro. Dec 12, 2012 at 12:23

7 Answers 7

156
const string message = "Hello" + ",world" + exclam;

The + operator has left-to-right associativity, so the equivalent parenthesized expression is:

const string message = (("Hello" + ",world") + exclam);

As you can see, the two string literals "Hello" and ",world" are "added" first, hence the error.

One of the first two strings being concatenated must be a std::string object:

const string message = string("Hello") + ",world" + exclam;

Alternatively, you can force the second + to be evaluated first by parenthesizing that part of the expression:

const string message = "Hello" + (",world" + exclam);

It makes sense that your first example (hello + ",world" + "!") works because the std::string (hello) is one of the arguments to the leftmost +. That + is evaluated, the result is a std::string object with the concatenated string, and that resulting std::string is then concatenated with the "!".


As for why you can't concatenate two string literals using +, it is because a string literal is just an array of characters (a const char [N] where N is the length of the string plus one, for the null terminator). When you use an array in most contexts, it is converted into a pointer to its initial element.

So, when you try to do "Hello" + ",world", what you're really trying to do is add two const char*s together, which isn't possible (what would it mean to add two pointers together?) and if it was it wouldn't do what you wanted it to do.


Note that you can concatenate string literals by placing them next to each other; for example, the following two are equivalent:

"Hello" ",world"
"Hello,world"

This is useful if you have a long string literal that you want to break up onto multiple lines. They have to be string literals, though: this won't work with const char* pointers or const char[N] arrays.

6
  • 3
    Also, const string message = "Hello" + (",world"+exclam); will work too, because of explicit parenthesization (is that a word?). May 19, 2011 at 16:22
  • 2
    Could be even more complete if you point out why the first example works: const string message = ((hello + ",world") + "!"); May 19, 2011 at 16:33
  • Thank you! I suspected it had something to do with left-to-right associativity but wasn't sure and this semantic difference didn't make much sense to me. I appreciate the answer! May 19, 2011 at 16:45
  • 4
    I’d mention the "Hello" ",world" syntax is useful not only for breaking into multiple lines but also when one of the string literals is a macro (or even both). Then the concatenation happens in compile time.
    – Melebius
    Oct 23, 2014 at 9:01
  • @Melebius Indeed! I only recently found out this is possible, and now feel extremely dumb to not learn it before =\ You can concatenate macro strings like this: #define (x) #x ## " is a foo." # - "stringifies" value, while ## concats 2 strings like this "x"" is a foo.". Jun 25, 2022 at 9:09
11

Since C++14 you can use two real string literals:

const string hello = "Hello"s;

const string message = hello + ",world"s + "!"s;

or

const string exclam = "!"s;

const string message = "Hello"s + ",world"s + exclam;
9

You should always pay attention to types.

Although they all seem like strings, "Hello" and ",world" are literals.

And in your example, exclam is a std::string object.

C++ has an operator overload that takes a std::string object and adds another string to it. When you concatenate a std::string object with a literal it will make the appropriate casting for the literal.

But if you try to concatenate two literals, the compiler won't be able to find an operator that takes two literals.

1
  • See std::operator+ which offers overloads for concatenating a std::string with another std::string, a character array, or a single character.
    – DavidRR
    Apr 28, 2015 at 14:55
9

Your second example does not work because there is no operator + for two string literals. Note that a string literal is not of type string, but instead is of type const char *. Your second example will work if you revise it like this:

const string message = string("Hello") + ",world" + exclam;
2

The difference between a string (or to be precise, std::string) and a character literal is that for the latter there is no + operator defined. This is why the second example fails.

In the first case, the compiler can find a suitable operator+ with the first argument being a string and the second a character literal (const char*) so it used that. The result of that operation is again a string, so it repeats the same trick when adding "!" to it.

1
  • To be pedantic, character literals do have + defined, just not with two char* terms. Otherwise, pointer arithmetic wouldn't work :) Apr 18, 2021 at 5:22
2

In case 1, because of order of operations you get:

(hello + ", world") + "!" which resolves to hello + "!" and finally to hello

In case 2, as James noted, you get:

("Hello" + ", world") + exclam which is the concat of 2 string literals.

Hope it's clear :)

0

if we write string s = "hello" + "world!"; RHS has following type const char [6] + const char [7]

Now both are built in data types. ie, they are not std::string types any more.

So, now operator overloading of built in types as defined by compiler applies. ie - no more operator + overloaded by std::string.

now let us turn to how compiler overloads

  • binary operator for two operands of const char * type.

it turns out, compiler did not overload for this case, as it is meaning less.

ie, adding two 'const char *' is semantically wrong as result would be another const char * in run time. There can be many reason why above does not make sense.

Hence over all, there is one generic rule for any operator overloading. it is : overloading any operator when all operands of that operator are built-in only. Compiler designers would take of such cases. In our exact question, std::string can't overload two 'const literals' because of this rule, and compiler choose to not to implement the + binary operator for its meaninglessness.

if we like the string literal form and we can a "s" operator as below. std::string p = "hello"s + "world!"s;

just suffix with s, the meaning changes. (s overloaded operator)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.