Reputation: 45
Using +
is a valid way to append a char to a string in C++ like so:
string s = "";
s += 'a';
However,
string s = "";
s += 'a' + 'b';
gives a warning: implicit conversion from 'int' to 'char' changes value
and does not append a
and b
characters.
Why does the first example append the char and the second does not?
Upvotes: 2
Views: 3422
Reputation: 3682
Change it to this:
s += 'a';
s += 'b';
In simple terms, the std::string::operator+=()
does not work as you wish because in the statement s += 'a' + 'b';
, the priority/precedence of the +
is greater than the assignment =
operator. This means that the compiler will first evaluate 'a' + 'b'
and will then evaluate the assignment =
. Thus it will assume that it's an addition of two char
s which usually is a 1-byte integer under the hood.
Adding the ASCII values of a
and b
will return 97+98 == 195 but the max value of char
is 255. So this will cause an overflow and then wrap around and will eventually result in 195-256 == -61 and -61 is not part of ASCII so what you will see depends on your specific terminal.
s += 'a' + 'b';
is like you're trying to add two ASCII values and then append the resulting char
(which is terminal-specific) to s
. And that's why you are not seeing your desired characters being printed.
When I run this code:
#include <iostream>
int main()
{
std::string str( "" );
str += 'a' + 'b';
std::cout << "The char is: " << str[ 0 ]
<< " and its ASCII value is: " << +str[ 0 ] << '\n';
return 0;
}
in the Windows command prompt I see the following:
The char is: ├ and its ASCII value is: -61
Hopefully, this helps.
Upvotes: -1
Reputation: 1
This has to do with operator precedence. Let's take for example the expression:
string s = "";
s += '0' + '1';//here + has higher precedence than +=
Here +
has higher precedence than +=
so the characters '0'
and '1'
are group together as ('0' + '1')
. So the above is equivalent to writing:
s+=('0' + '1');
Next, there is implicit conversion from char
to int
of both '0'
and '1'
. So '0'
becomes the decimal 48 while the character '1'
becomes decimal 49.
So essentially the above reduces to:
s+=(48 + 49);
or
s+=(97);
Now 97 corresponds to the character a
and therefore the final result that is appended to the string variable named s
is the character a
.
Now lets apply this to your example:
string s = "";
s += 'a' + 'b';
First due to precedence:
s+=('a' + 'b');
Second implicit conversion from char
to int
happens:
s+=(97 + 98);
So
s+=195;
So the character corresponding to decimal 195 will be appended to string s.
You can try out adding and subtracting different characters to confirm that this is happening.
Why does the first example append the char and the second does not?
So the fundamental reason is operator precedence.
Upvotes: 3
Reputation: 234865
That's a drawback with operator overloading; one needs to remember it is little more than a syntactic sugar and a nice way of calling functions.
s += 'a' + 'b';
is grouped as it would be for a simple expression like
a += 1 + 2;
for an int
type a
.
In other words 'a' + 'b'
is evaluated first. And that has an int
type due to argument promotion rules. A std::string
has no specific overload for +=
for an int
, only one that is normally a narrowing conversion, so the compiler issues a warning.
If you want to append "ab"
to the string s
, then use
s += "ab";
Upvotes: 1