Strange behaviour of << (as at least seems to me)
Asked Answered
G

3

5

I can't realize how could it be possible to print a string this way without any complaint by the compiler:

std::cout << "Hello " "World!";

In fact, the above line works exactly like:

std::cout << "Hello " << "World!";

Is there an explanation for this behaviour?

Genseric answered 9/10, 2012 at 13:59 Comment(1)
Actually, it behaves exactly like std::cout << "Hello World!"; The second version above calls the stream inserter twice, while the first calls it once.Prosector
Q
11

Adjacent literal tokens are concatenated automatically, it's part of the standard.

2.1 Phases of translation [lex.phases]

6) Adjacent ordinary string literal tokens are concatenated. Adjacent wide string literal tokens are concatenated.

(C++03)

Quotidian answered 9/10, 2012 at 13:59 Comment(10)
Thanks... I'm moving from Java to C++... Never heard about something like this before.Genseric
So it is the same for integer literals: 'int i = 123 456 789; // int i = 123456789;Genseric
@Genseric what's an integer literal?Quotidian
@Luchian: if that's not a trick question, then integer literals are defined in 2.13.1 of the C++03 standard. Why have you crossed out some of the text? By doing so you incorrectly suggest that literal tokens other than string literals are concatenated.Bayonne
I thought this also could be right... but it's not. int i = 123 456 789; cout << i; // will print 123456789Genseric
@SteveJessop I didn't treat it as a trick question, I've never heard of integer literals before. Also, those lines are crossed out in my version of the standard, that's why I also crossed them out here.Quotidian
@Luchian: maybe the reason you haven't heard the term is just that the thing that in C++ is called an "integer literal" is called in C an "integer constant", not a "literal" at all.Bayonne
@LuchianGrigore - what you're looking at is your version of not the standard; that's why things are crossed out. The C++03 version is what you quoted, including the text that you show as crossed out; the C++11 version says "Adjacent string literal tokens are concatenated." That change was made because of the proliferation of string literal token types; the details of concatenation are now covered in [lex.string].Prosector
@PeteBecker yes, it's a draft (and already noted that it's a C++03 one). I thought the crossed out parts are changes.Quotidian
@LuchianGrigore - the crossed out parts mark changes from the previous draft. They don't tell you what ended up in the final version, or even in the next draft.Prosector
H
1

In C++, literals tokens can be concatenated thusly:

const char* thingy = "Hello" "World";

"Hello" and "World" are each a literal token.

Holloway answered 9/10, 2012 at 14:0 Comment(0)
T
1

This is normal behavior of the strings. In the first line specified strings are concatenated by compiler automatically. As sample you can specify also multiline to avoid very long line.

const char *strLine = "line 1 "
                      "line 1 "
                      "line 2 ";

And it will work OK. The second line is cleared, specified another line for output.

Topflight answered 9/10, 2012 at 14:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.