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)
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)
If I understand you correctly, I believe you want this:
#include <iostream>
...
std::cout << std::endl << "yadayadayada" << std::endl;
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++.
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.
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.
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.
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. :(
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.
#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)