0

Is it a good way to concatenate string literals as it's done in the following snippet?

outputStream << "[" "]";

I am actually a bit surprised that this statement compiles and works as expected.

J.John
  • 101
  • It works, whether it's good style of not really depends on the context. I'd not write the exact code you've posted myself. It is a reasonable way to split up a very long string literal so that you don't have to scroll across the page to see it for example. – john Jul 11 '20 at 17:37
  • In your case it makes no sense, but in general there's nothing wrong with it. Normally you see it in macros, or when splitting long literals into several lines. – HolyBlackCat Jul 11 '20 at 17:38
  • The dupe is for C language, but string literals concatenation works exactly the same in C++ as in C. Basically adjacent string literals are concatenated into one. – rustyx Jul 11 '20 at 17:38
  • It is more usual when the string is very long onto several lines. Also good for formatting code such as SQL in the C++. – QuentinUK Jul 11 '20 at 17:39
  • at least gcc replace "[" "]" by "[]", so does the concatenation at compile time. I use that often when I have a long string to make the code more clear, allowing to put the string on several each on a line and indent them – bruno Jul 11 '20 at 17:39
  • Welcome to StackOverflow! I see you're a new contributor, so I advise you to check out [How to ask a good question](https://stackoverflow.com/help/how-to-ask) and [How to create a minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). Can you clarify what you mean by "good"? If you're asking whether this behavior is portable, then it is true that adjacent strings literals are [guaranteed to be concatenated](https://en.cppreference.com/w/cpp/language/translation_phases#Phase_6) during compilation by any standard compliant C++ compiler. – Brian61354270 Jul 11 '20 at 17:40

0 Answers0