Is this legal under C++11?
string s = R"(This is the first line And this is the second line)";
... being equivalent to:
string s = "This is the first line\nAnd this is the second line";
Raw StringsThey can span multiple lines without concatenation and they don't use escaped sequences. You can use backslashes or double quotes directly.
We can use string literal concatenation. Multiple string literals in a row are joined together: char* my_str = "Here is the first line." "Here is the second line."; But wait!
Raw String Literal in C++ A Literal is a constant variable whose value does not change during the lifetime of the program. Whereas, a raw string literal is a string in which the escape characters like ' \n, \t, or \” ' of C++ are not processed. Hence, a raw string literal that starts with R”( and ends in )”.
Use the std::string Class to Declare Multiline String in C++C++ allows multiple double-quoted string literals to be concatenated automatically in a statement. As a result, one might include any number of lines while initializing the string variable and keep the code more consistently readable.
So here we have some reasons why raw string literals are introduced in C# 11. Inside a raw string literal you can just use the special characters (for example a double quote ( " )) without the need to escape them with a backslash ( \) or a double quote ( " ). Next to that you can also use multiple lines.
What ways do we have to define large string literals in C? Let’s take this example: #include <stdio.h> char* my_str = "Here is the first line. Here is the second line."; int main (void) { printf ("%s ", my_str); return 0; } We could first try to split this up as: char* my_str = "Here is the first line. Here is the second line.";
In C# 11 you can start a raw string literal by typing at least 3 double quotes ( """ ). The raw string literal ends when you use 3 double quotes again. Inside the raw string literal you can use the double quotes and other special characters without the need to escape them.
Apparently in C++11 and in GCC with extensions, we can write “raw strings” like this: char* my_str = R"Here is the first line. Here is the second line."; However, clang doesn’t seem to like this.
Yes, that is perfectly valid. See here.
Also, from the (draft) standard 2.14.5/4:
A source-file new-line in a raw string literal results in a new-line in the resulting execution string-literal. Assuming no whitespace at the beginning of lines in the following example, the assert will succeed:
const char *p = R"(a\ b c)"; assert(std::strcmp(p, "a\\\nb\nc") == 0);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With