Reputation: 1776
I'm studying about the C++ library type string. I was looking into operation that the type can perform, among which are concatenation. I know that in C++ operators can be overloaded to suit class types needs,and I know that character string literals are const char arrays under the hood. So, I know that the string type defines a constructor that takes a string literal argument and converts it into a string, so that I can initialize a string using:
string s="hello!"
what happens here is an impicit conversion (through the constructor that takes a const char*) to string and then a copy initialization is performed (through the copy-constructor of string).
so far so good.. (correct me if I am getting something wrong so far)
Now, I know that I can concatenate two strings, or I can concatenate a string and a char literal (or vice versa) and a string and a string literal (so I suppose the first happens because there's an overloaded + operator that takes symmetrically a char and a string, the latter happens thanks to the overloaded + opeartor that concatenates two strings and the implicit conversion from const char* to string). My questions are:
1: why can't I concatenate two string literals? shouldn't the operator+ that takes two strings be called (and two implicit conversion performed from const char* to string) ?
string s="hi"+" there";
2: I tried to concatenate a char literal and a string literal (calling the operator+ that takes a char and a string) but I get only garbage in the resulting string, as the compiler doesn't mind, but the result is certainly not what I want.
string s='h'+"ello!";
cout<<s<<endl;
I get garbage out.If a operator+(char,const string&) exists it should be called after implicitly converting "ello" to string shouldn't it?
can you please explain?
Upvotes: 1
Views: 1490
Reputation: 70381
1) Concatenating two string literals.
"Hello" + " world"
The compiler has no hint that it should be looking for anything related to std::string
-- it is looking at two const char[]
objects, and you cannot +
those (or the const char *
they could be degraded to).
2) Using
operator+
on a char literal and a string literal.
(Thanks to User dyp for pointing in the right direction.)
If you literally mean a char
literal -- e.g. 'a'
-- then you're running afoul of one of the more surprising things C/C++ have to offer.
Consider: a[i]
is equivalent to i[a]
. (This is a fact.)
So, if you write:
'a' + "Hello"
...that is equivalent to (in ASCII)...
"Hello" + 97
...which is a pointer into nowhere, i.e. constructing a std::string
from it is undefined behaviour.
Upvotes: 2
Reputation: 149155
Borgleader already gave half the answer in comment:
when adding two string litterals, compiler only sees two const char *
and they could be cast to string but also to integers. So unless you explicitely say that you want them as strings, you get an error
when adding a string litteral and a char, compiler sees a pointer and an integral number. It knows how to process that and does pointer arithmetics on your C string - which is probably not what you expected! There are chances that you end past the end of the char array and invoke undefined behaviour. Examples:
"abc" + '0' => "abc" + 48 : 44 positions past the NULL ...
"abcdefghijabcdefghijabcdefghijabcdefghijabcdefghij" + '0' => "ij"
Upvotes: 1