Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

cout

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?

like image 304
maurizeio Avatar asked Oct 09 '12 13:10

maurizeio


2 Answers

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)

like image 155
Luchian Grigore Avatar answered Oct 05 '22 08:10

Luchian Grigore


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

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

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

like image 45
John Dibling Avatar answered Oct 05 '22 08:10

John Dibling