Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What exactly happens when a character is appended to a string literal with the '+' operator in C++?

Tags:

c++

string

stl

Based on my reading, the following code:

string aggregate = "give" + 'n';

Should produce a resulting string with the value:

"given".

It instead produces garbage. Why doesn't the following happen?

  1. "give" is converted to a std::string via the constructor that takes a pointer to a character array.

  2. The '+' overload that takes a std::string and a character is called, returning a new string.

I am basing my theory on this man page.

Now, I have heard that the first argument to an overloaded operator isn't a candidate for constructor conversion if the operator is a member of a class. I believe I read that in Koenig and Moo. But, in this case I understand the '+' operator to be a non-member overload.

I realize this seems like a ridiculous over-complication, but I like to know FOR SURE what is happening when I write code.

like image 353
gonzo_taylor Avatar asked Dec 13 '22 15:12

gonzo_taylor


2 Answers

The expression "give" + 'n' is evaluated first, adding (int)'n' to the address of the string constant "give". The result of this pointer arithmatic is assigned to the string object. No error is raised because the result is of type const char*.

You should get used to thinking of everything on the right of the = as happening before the actual assignment (or initialization in this case).

You want the following:

// construct the string object first
std::string aggregate = "give";

// then append the character
aggregate += 'n';

or, explicitly build the a string object first:

std::string aggregate = std::string("give") + 'n';
like image 194
meagar Avatar answered May 22 '23 10:05

meagar


The fact that there is a std::string on the left side of the expression doesn't matter for the first step: "give" is a const char[5] which decays to a const char* and 'n' is a char, that is, an integer type. Adding an integer to a pointer simply adds to the pointer's address. The result is again of type const char*, which is then implicitly converted to std::string.

Basically, your code is equivalent to:

const char* c = "a string" + 'n';
std::string s = c;

So, to achieve the desired effect, try

std::string s = std::string("a string") + 'n';
like image 24
Alexander Gessler Avatar answered May 22 '23 08:05

Alexander Gessler