C++: Here Document

Bjarne-stroustrup
 

A here document (or “heredoc”) is a way of specifying a text block, preserving the line breaks, indentation and other whitespace within the text.

Depending on the language being used a here document is constructed using a command followed by “<<” (or some other symbol) followed by a token string.

The text block will then start on the next line, and will be followed by the chosen token at the beginning of the following line, which is used to mark the end of the textblock.

The task is to demonstrate the use of here documents within the language.

C++11 raw string literals are similar to heredocs, except there is no newline after the opening token or before the ending token (unless you actually want newlines there).

#include <iostream> // Only for cout to demonstrate

int main()
{
	std::cout <<
	R"EOF(  A  raw  string  begins  with  R,  then a double-quote ("),  then an optional
	identifier (here I've used "EOF"),  then an opening parenthesis ('(').  If you
use  an  identifier,  it  cannot  be longer than 16 characters,  and it cannot
contain a space,  either opening or closing parentheses, a backslash, a tab, a
vertical tab, a form feed, or a newline.

It  ends with a closing parenthesis (')'),  the identifer (if you used one),
and a double-quote.

All  characters are okay in a raw string,  no escape sequences are necessary
or recognized, and all whitespace is preserved.
)EOF";
}

SOURCE

Content is available under GNU Free Documentation License 1.2.

Subscribe to TFE Times

Enter your email address to become a member of TFE Times today!