What are raw literals in C ++
C ++ multiline string literal
Is there a way to use multiline, constant plaintext literals in C ++ à la Perl? Maybe a parsing trick with a file? I can't imagine one, but boy, that would be nice. I know it will be in C ++ 0x.
Well, something like that. It is easiest to just use the fact that adjacent string literals are concatenated by the compiler:
The indentation doesn't matter because it's not in the quotation marks.
You can do this too, as long as you take care to bypass the embedded line break. If you don't, as my first answer did, the following won't compile:const char * text2 = "On the other hand, I've gone mad here \ and really let the literal span multiple lines \ without bothering to quote the contents \ of each line. That works, but you can't indent.";
Again, note these backslashes at the end of each line. You must be just before the end of the line and escape the new line in the source for everything to act as if the new line didn't exist. There will be no line breaks in the string where you had backslashes. Obviously, this form does not allow you to indent the text as the indentation then becomes part of the string and is given random spaces.
In C ++ 11, you have raw string literals. Kind of like here text in shells and scripting languages like Python and Perl and Ruby.
All spaces and indentations as well as line breaks in the string are retained.
This can also be utf-8 | 16 | 32 or wchar_t (with the usual prefixes).
I would like to point out that the escape sequence V0G0N is actually not needed here. His presence would make it possible to put on the string. In other words, I could have put it
(note additional quotation marks) and the above string would still be correct. Otherwise I might as well have used it
The parens directly in the quotation marks are still required.
Consumes everything between the brackets.
Replaces any number of consecutive spaces with a single space.
Perhaps a convenient way to enter multiline strings is to use macros. This only works if the quotes and brackets are balanced and not contain top-level commas:
Compiled with gcc 4.6 or g ++ 4.6 this gives:
Note that this cannot be included in the string unless it is enclosed in parentheses or quotation marks. Single quotation marks can be used, but generate compiler warnings.
To edit: As mentioned in the comments, the use of.
You can also do this:
You can just do this:
Since an ounce of experience is worth a lot of theory, I tried a small test program for:
Also compile this fragment in order to reproduce it.
The trick behind this is that the comma separator is not processed. So you can pass it to the stringing operator. Leading and trailing spaces are truncated, and spaces (including line breaks) between words are then compressed into a single space. Brackets need to be balanced. I think these flaws explain why the designers of C ++ 11 saw it despite the need for raw string literals.
Just to explain a little the comment from @ emsr in the answer from @ unwind, if you are not lucky enough to have a C ++ 11 compiler (e.g. GCC 4.2.1) and the line breaks in the string would like to embed (either char * or class string) you can write something like this:
Very obvious, true, but I didn't notice @ emsr's brief comment the first time I read it, so I had to figure that out for myself. Hopefully I saved someone else a few minutes.
Option 1. Using the Boost library, you can declare the string as follows
Option 2. If boost is not available in your project, you can use std :: string_view () in modern C ++.
- Is ethical consumption a contradiction?
- Why do guitarists use capos?
- How do we construct spur gears
- What is the New York Giants Rating
- Can transfer students joining fraternities
- What are Greek pillars made of
- Would GST benefit customers
- How is the Linux operating system used
- How did Mercator create a map
- Why do guitarists use capos?
- Deleting a Mac removes viruses
- How do you predict a flood?
- Why do some people demonize atheists
- When is R squared negative
- How doctors split conjoined twins
- Really cries srk in his movies
- How are Trump's lies about Russia
- What is peace
- Why is someone beautiful
- How do doctors treat narcotics addiction
- How safe is Maruti Suzuki brezza
- Will my industrial piercing close?
- Carpooling is good for my sanity