Is there any way to have multi-line plain-text, constant literals in C++, Ã la Perl? Maybe some parsing trick with
#includeing a file? I can’t think of one, but boy, that would be nice. I know it’ll be in C++0x.
Well … Sort of. The easiest is to just use the fact that adjacent string literals are concatenated by the compiler:
const char *text = "This text is pretty long, but will be " "concatenated into just a single string. " "The disadvantage is that you have to quote " "each part, and newlines must be literal as " "usual.";
The indentation doesn’t matter, since it’s not inside the quotes.
You can also do this, as long as you take care to escape the embedded newline. Failure to do so, like my first answer did, will not compile:
const char *text2 = "Here, on the other hand, I've gone crazy and really let the literal span several lines, without bothering with quoting each line's content. This works, but you can't indent.";
Again, note those backslashes at the end of each line, they must be immediately before the line ends, they are escaping the newline in the source, so that everything acts as if the newline wasn’t there. You don’t get newlines in the string at the locations where you had backslashes. With this form, you obviously can’t indent the text since the indentation would then become part of the string, garbling it with random spaces.