C++ Reference ยท Types and Language Concepts
Raw string literals
Reference for C++ raw string literals R"(...)": no escape sequences, custom delimiters, multi-line text, regex and JSON examples, and pitfalls.
A raw string literal is a string literal in which backslashes and quotes are taken literally. You write R"( to open it and )" to close it, and everything in between is content, including newlines. This removes the "escape everything twice" problem in regular expressions, Windows paths, JSON, SQL, HTML and shell snippets.
Raw strings exist since C++11 and combine with the encoding prefixes: u8R"(...)", uR"(...)", UR"(...)", LR"(...)". If the text itself contains the closing sequence )", add a custom delimiter of up to 16 characters between the quote and the parenthesis.
const char* path = R"(C:\Users\Ada\notes.txt)"; const char* json = R"({"name": "Ada", "id": 7})"; const char* tricky = R"xyz(contains )" safely)xyz";
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.
Syntax
R"delimiter( raw characters )delimiter" u8R"(...)" uR"(...)" UR"(...)" LR"(...)"
| Part | Description |
|---|---|
R |
Marks the literal as raw. May follow an encoding prefix (u8, u, U, L). |
delimiter |
Zero to 16 characters, chosen from the basic character set but not spaces, parentheses, backslash or control characters. Must be identical at both ends. |
( ... ) |
The content. It ends at the first ) followed by the same delimiter and ". |
Errors / undefined behavior: a literal that is not terminated, a delimiter longer than 16 characters, or a mismatched delimiter is a compile error. Like other string literals, a raw string literal has type const char[N] and must not be modified.
Usage notes
- Escape sequences are not processed:
R"(\n)"is two characters, a backslash and ann. The only text that has special meaning is the closing)delimiter". - Whitespace is preserved, including leading indentation of continuation lines and the newline right after
R"(. Start the content on the same line as the opening if you do not want an initial newline. - Newline translation. The source file's newline is converted to
\nby the compiler, even when the file uses CRLF, so the content is the same on every platform. - Delimiters are the way to embed
)". Give them a meaningful name (json,sql,html) and your editor may highlight the content. - Preprocessing: raw strings are recognized before trigraphs, line splices (
\at end of line) and other translation phases are applied, so a backslash-newline in a raw string stays as is. - Not a
std::string. The type is a character array; wrap it instd::string, use thessuffix (R"(text)"s), orstd::string_viewas shown. - Formatting with
std::formatworks with raw literals too, but braces{}in the content are format placeholders: write{{and}}for JSON braces. - Regular expressions: the raw literal removes only the C++ level of escaping; the regex engine still processes its own escapes.
Related
- Literals,
std::regex,std::string,std::string_view - Lessons: Strings, Regular expressions