Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <pre><code>const string message = "Hello" + ",world" + exclam; </code></pre> <p>The <code>+</code> operator has left-to-right associativity, so the equivalent parenthesized expression is:</p> <pre><code>const string message = (("Hello" + ",world") + exclam); </code></pre> <p>As you can see, the two string literals <code>"Hello"</code> and <code>",world"</code> are "added" first, hence the error.</p> <p>One of the first two strings being concatenated must be a <code>std::string</code> object:</p> <pre><code>const string message = string("Hello") + ",world" + exclam; </code></pre> <p>Alternatively, you can force the second <code>+</code> to be evaluated first by parenthesizing that part of the expression:</p> <pre><code>const string message = "Hello" + (",world" + exclam); </code></pre> <p>It makes sense that your first example (<code>hello + ",world" + "!"</code>) works because the <code>std::string</code> (<code>hello</code>) is one of the arguments to the leftmost <code>+</code>. That <code>+</code> is evaluated, the result is a <code>std::string</code> object with the concatenated string, and that resulting <code>std::string</code> is then concatenated with the <code>"!"</code>.</p> <hr> <p>As for <em>why</em> you can't concatenate two string literals using <code>+</code>, it is because a string literal is just an array of characters (a <code>const char [N]</code> where <code>N</code> 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. </p> <p>So, when you try to do <code>"Hello" + ",world"</code>, what you're really trying to do is add two <code>const char*</code>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. </p> <hr> <p>Note that you <em>can</em> concatenate string literals by placing them next to each other; for example, the following two are equivalent:</p> <pre><code>"Hello" ",world" "Hello,world" </code></pre> <p>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 <code>const char*</code> pointers or <code>const char[N]</code> arrays.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload