Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In C++11, can raw string literals have multiple lines?

Tags:

c++

string

c++11

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"; 
like image 943
Janik Zikovsky Avatar asked Mar 26 '14 17:03

Janik Zikovsky


People also ask

Can a string have multiple lines?

Raw StringsThey can span multiple lines without concatenation and they don't use escaped sequences. You can use backslashes or double quotes directly.

How do I write a multi line string literal in C?

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!

What is a raw string literal?

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 )”.

Can a string be multiple lines C++?

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.

Why are raw string literals introduced in C#11?

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.

How to define large string literals in C?

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.";

How do you write a string in C with 3 quotes?

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.

Is it possible to write a string as a raw string?

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.


1 Answers

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); 
like image 113
Ferruccio Avatar answered Sep 22 '22 02:09

Ferruccio