tags:

views:

85

answers:

2

Possible Duplicate:
Something like print END << END; in C++?

In a shell script or in a perl program the so called "HERE" documents are commonly used for longer text, e.g. Perl:

my $t=<<'...';

usage:

   program [options] arg1 arg2

      options:

            -opt1  description for opt1
            -opt2  description for opt2
...

print $t;

This style is very well readable, e.g. no need to escape quotes or to explicitly insert \n.

I am wondering if there is a comparable elegant approach to embed a longer text inside a C/C++ program?

#include <iostream>;
int main(void) {
  std::string t;
  // t = ... the same long text as in the perl example in a HERE document fashion ...
  std::cout << t;
  return 0;    
}

EDIT: Simplification: there is no variable interpolation needed.

+3  A: 

Unfortunately there's no elegant solution. I keep using:

std::string lorem =
"Lorem ipsum dolor sit amet, consectetur adipisicing elit, "
"sed do eiusmod tempor incididunt ut labore et dolore magna "
"aliqua. Ut enim ad minim veniam, quis nostrud exercitation "
"...";

C/C++ sticks the strings together, unfortunately there's no way to enter a linebreak implicitly except using \n.

Besides, this is a duplicate of this SO question.

Kornel Kisielewicz
Would not compile. One of the operands have to be std::string, or, better, remove the plus signs.
Pavel Radzivilovsky
Made a typo, sorry.
Kornel Kisielewicz
Sorry about the duplicate. I searched beforehand, but I didn't find it.
+3  A: 

I have always used

"....\n"
".....\n"
"....\n"

Which is actually one char* literal, and has the little advantage of not getting distorted when MSVC optimizes tabs.

Pavel Radzivilovsky