concatenating strings & string literals

A

arnuld

i tried to output these 2 to the std. output:

const std::string hello = "Hello";
const std::string message = hello + ", world" + "!";

const std::string exclam = "!";
const std::string message2 = "hello" + " world" + exclam;


the first one runs fine but 2nd does not as we can not combine 2
string literals. i know that the "+" operator is left-associative,
hence:

/hello + ", world" + "!"/

means

/(hello + ",world") + "!"/

but what does /(hello + ", world")/ will produce:

a string literal
a character literal
or something else ?
 
M

Michael DOUBEZ

arnuld a écrit :
i tried to output these 2 to the std. output:

const std::string hello = "Hello";
const std::string message = hello + ", world" + "!";

const std::string exclam = "!";
const std::string message2 = "hello" + " world" + exclam;


the first one runs fine but 2nd does not as we can not combine 2
string literals. i know that the "+" operator is left-associative,
hence:

/hello + ", world" + "!"/

means

/(hello + ",world") + "!"/

but what does /(hello + ", world")/ will produce:


a string literal
a character literal
or something else ?


It will produce a temporary std::string object.
The function called is
std::string operator+(const std::string& lhs, const char* rhs);

So what your line do is in fact:
message = operator+( operator+( hello , ", world") , "!" );



Michael
 
A

Alan Johnson

arnuld said:
i tried to output these 2 to the std. output:

const std::string hello = "Hello";
const std::string message = hello + ", world" + "!";

const std::string exclam = "!";
const std::string message2 = "hello" + " world" + exclam;


the first one runs fine but 2nd does not as we can not combine 2
string literals. i know that the "+" operator is left-associative,
hence:

/hello + ", world" + "!"/

means

/(hello + ",world") + "!"/

but what does /(hello + ", world")/ will produce:

a string literal
a character literal
or something else ?

It is probably worth noting that adjacent string literals are
automatically concatenated. So you could write the second example as:

const std::string exclam = "!";
const std::string message2 = "hello" " world" + exclam;

Of course, you can usually just concatenate such literals yourself, but
sometimes this is convenient (e.g. breaking a literal across multiple
lines of source code).
 
G

Georg Krichel

arnuld said:
i tried to output these 2 to the std. output:

const std::string hello = "Hello";
const std::string message = hello + ", world" + "!";

const std::string exclam = "!";
const std::string message2 = "hello" + " world" + exclam;


the first one runs fine but 2nd does not as we can not combine 2
string literals. i know that the "+" operator is left-associative,
hence:

/hello + ", world" + "!"/

means

/(hello + ",world") + "!"/

but what does /(hello + ", world")/ will produce:

a string literal
a character literal
or something else ?
The two literals in the 2nd example are internally represented as pointers
to char. So what you do is pointer arithmetics on char pointers, and then
you concatenate the resulting pointer with exclam using

std::string operator +( const char* lhs, const std::string& rhs )

Georg
 
A

arnuld

const std::string hello = "Hello";
The two literals in the 2nd example are internally represented as pointers
to char.


i did not know that but i understand when you said that
So what you do is pointer arithmetics on char pointers, and then
you concatenate the resulting pointer with exclam using
std::string operator +( const char* lhs, const std::string& rhs )

out of my head


George, i guess, you have learnt C

?

-- arnuld
 
A

Alf P. Steinbach

* arnuld:
i tried to output these 2 to the std. output:

const std::string hello = "Hello";
const std::string message = hello + ", world" + "!";

const std::string exclam = "!";
const std::string message2 = "hello" + " world" + exclam;


the first one runs fine but 2nd does not as we can not combine 2
string literals. i know that the "+" operator is left-associative,
hence:

/hello + ", world" + "!"/

means

/(hello + ",world") + "!"/

but what does /(hello + ", world")/ will produce:

a string literal
a character literal
or something else ?

Just try to compile it.
 
A

arnuld

Just try to compile it.


i compiled it before posting here. it runs without any error. 2nd one
gives an error, which is obvious as one can *not* combine "string
literals".

BTW, why you asked about compilation?

i never post anything without compiling 1st (& then thinking it over,
if it produces errors). only after that i post something here.
 
A

Alf P. Steinbach

* arnuld:
i compiled it before posting here. it runs without any error. 2nd one
gives an error, which is obvious as one can *not* combine "string
literals".

BTW, why you asked about compilation?

Because you posted code that shouldn't compile.

i never post anything without compiling 1st (& then thinking it over,
if it produces errors). only after that i post something here.

Then it seems your question is not about the two first code example, but
literally what you wrote, 'what does /(hello + ", world")/ will
produce'. And here you combine a std::string and a string literal via
the + operator. If you have included <string>, then you have an
overload of that operator that takes these args and produces a std::string.
 
O

Old Wolf

The two literals in the 2nd example are internally represented as pointers
to char. So what you do is pointer arithmetics on char pointers,

Actually they are arrays of char, and are represented
as such. They are not converted into pointers until you
use the '+' operator on them.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top