16

Why it is possible to do

const string exclam = "!"; const string str = exclam + "Hello" + " world"; 

And not possible to do this:

const string exclam = "!"; const string str = "Hello" + " world" + exclam; 

I know (although can't understand why) that it is not allowed to do:

const string str = "Hello" + " world" + "!"; 

as it will be interpreted like const char[6] + const char[6] + const char[1], so from other side, why this is not allowed also, or why it uses char[] and not string.

2
  • 4
    If you want to concatenate string literals, then don't use +: const string str = "Hello" " world" + exclam; Commented Aug 29, 2010 at 8:31
  • also checkout onlinegdb.com/rJxM4IugX Commented Jun 8, 2018 at 19:08

5 Answers 5

19

The + operator is left-associative (evaluated left-to-right), so the leftmost + is evaluated first.

exclam is a std::string object that overloads operator+ so that both of the following perform concatenation:

exclam + "Hello" "Hello" + exclam 

Both of these return a std::string object containing the concatenated string.

However, if the first two thing being "added" are string literals, as in:

"Hello" + "World" 

there is no class type object involved (there is no std::string here). The string literals are converted to pointers and there is no built-in operator+ for pointers.

Sign up to request clarification or add additional context in comments.

4 Comments

@sbi : Operators never get evaluated, operands do.
@Prasoon: A valid nitpick which I can't do anything about because I cannot edit that comment anymore. (However, you don't answer my implied question.)
@sbi: Such operators are called "left associative" in English too. When I originally posted, I couldn't remember whether "X associative" meant the "X-most" operator was evaluated first or last (where "X" is "left" or "right"). Thanks. @Prasoon: Technically, expressions get evaluated :-)
Thanks, now I know for sure! P.S.: I removed my comment, because now you incorporated in into your answer.
3

It's because you are concatanating const char[6] + const char[6], which is not allowed, as you said.

In C++, string literals (stuff between quotes) are interpreted as const char[]s.

You can concatenate a string with a const char[] (and vice-versa) because the + operator is overridden in string, but it can't be overridden for a basic type.

1 Comment

@Philip: A string literal has the type const char[N] where N is the size of the string literal. It is an array, and like an array it can be implicitly converted to a pointer to its first element (a const char*). In C++, while the conversion of a string literal to (non-const) char* is deprecated, it is still allowed.
3
const string exclam = "!"; // Initialize a c++ string with an ansi c string const string str = exclam + "Hello" + " world"; // Using the operator+ method of exclam 

You can do it because the operator+ of exclam will return a new string containing "!Hello", on which you subsequently call the operator+ with " world" as parameter, so you get another string which, finally, gets assigned to str by means of the copy constructor.

On the other hand

const string str = "Hello" + " world" + exclam; 

cannot be executed because "Hello" is just a const char *, which doesn't have a operator+ taking a const char * as parameter.

Comments

1

(New answer, this was not possible back in 2010)

You can now write

const string str = "Hello"s + " world"s + "!"s; // ^ ^ ^ 

By adding that s after a string literal, you tell the compiler it's actually a std::string and not a const char[]. That means you can call members functions on it. E.g. ("ABC"s).back() but also +

1 Comment

0

In addition to the answers that explain the reason for your observations, I post here how to get rid of the problem (you might have figured this out already).

Replace

const string str = "Hello" + " world" + exclam; 

with

const string str = string("Hello") + " world" + exclam; 

so you make sure the first operand is a string and not a const char[].

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.