views:

201

answers:

8

Is there anyway to do something like PHP's

print << END
yadayadayada
END;

in C++? (multi-line, unescaped, easy-to-cut-and-paste stream insertion)

A: 

If I understand you correctly, I believe you want this:

#include <iostream>
...
std::cout << std::endl << "yadayadayada" << std::endl;
SingleShot
not at all i'm afraid, I guess I didn't explain that clearly. I want something that can cout (or insert to any stream) UN-escaped text. in PHP it's done like I said (im pretty sure, it's been a while), and it will print anything exactly, until it finds "END;" on a new line, on its own. actually, you can use whatever end "deliminer" you want to declare, but it has to end with a semicolon and on its own line.
Nona Urbiz
+2  A: 

You can do like this:

std::cout << "First line\n"
"second line\n"
"third line\n" ;

And that's the best you can do with C++.

Pavel Shved
so the answer is that it's impossible?
Nona Urbiz
+7  A: 

This is the best you can do:

std::cout <<
    "This is a\n"
    "multiline\n"
    "string.\n";

Not as convenient as a proper heredoc, but not terrible.

RichieHindle
It should be noted that this isn't really any special multiline string syntax. The C and C++ languages will concatenate string literals in source code, so this is identical to `std::cout << "This is a\nmultiline\nstring.\n";` in every respect (except readability).
Chris Lutz
A: 

There is not such thing as a HEREDOC in C++.

You can do

cout << 
"yadayadayada"
"yadayadayada"
<< endl;

That is the best you can get. But you still has to escape special chars like \,",'.

The way would it be to include the text as an ressource into the executable or load it from an external file.

codymanix
+8  A: 

In C++, it's not usually considered code style to put large amounts of data into source code so there isn't a fancy language way to do it.

It is usually more flexible to put the text into an external file (such as a text file), then it isn't bound into the compiled executable.

If you do want the text to be bound into the executable then (depending on your platform) you can often use some form of resource support, or an assembler with an 'incbin' style directive to give name to a data area with the text that you want.

Alternatively, you can use an external utility (such as xxd -i) to compiler a named C style array from a given input file. The generated file can then be compiled with the rest of the source code.

Charles Bailey
"In C++, it's not usually considered code style to put large amounts of data into source code so there isn't a fancy language way to do it." I don't think that's the reason why there's no fancy way to do it, but +1 for `xxd`
Chris Lutz
+2  A: 

The next C++ standard, referred to as C++1x (or C++0x, if you're into hex), will have raw string literals:

// this doesn't have '\n', but '\\' and 'n'
R"[yada"yadayada\n]"

And if you need those brackets, you can do that, too, using whatever you want for an end token:

// the follwoing will be "[yada][yada][yada]"
R"END[[yada][yada][yada]]END"

Unfortunately, I don't think there's a C++ compiler out there already supporting this. :(

sbi
But I think that still won't allow embedded newlines.
me22
In fact it does. I'll add an example for this.
sbi
A: 

A slash at the end of a line means the next line is a continuation of the string. This leads you to an alternate syntax that you may or may not prefer over other suggestions:

std::cout << "\
This is a\n\
multiline\n\
string.\
";

The advantages: you don't have to wrap each line in quotes; when importing text from some other source you only have to edit one end of each line, not both ends (more macro friendly); and you can see exactly how the resulting string will look, including leading spaces.

The disadvantages: you still have to explicitly include \n for newlines, and I don't like losing the ability to indent my code nice and pretty. HEREDOC syntax also suffers from this feature, so perhaps that won't bother you.

Personally, this isn't a syntax I like.

Darryl
A: 
#define TEXT(x) #x

std::cout << TEXT(
  blah blah
  blah blah
  blah blah
  blah blah
);

(Meant as a joke, of course, not a serious suggestion)

Idelic