escape R"()" in a raw string in C++
Asked Answered
D

2

24
  string raw_str = R"(R"(foo)")";

If I have R"()" inside a raw string, and that causes the parser to confuse. (ie., it thought the left most )" was the end of the raw string.

How do I escape this?

Dome answered 21/3, 2018 at 21:15 Comment(1)
Possible duplicate of Include )" in raw string literal without terminating said literalWristlet
W
39

The format for the raw-string literals[2] is: R"delimiter( raw_characters )delimiter"

so you can use a different delimiter that is not in the string like:

string raw_str = R"~(R"(foo)")~";
Wristlet answered 21/3, 2018 at 21:20 Comment(0)
C
14

The raw string will terminate after the first )" it sees. You can change the delimiter to *** for example:

string raw_str = R"***(R"(foo)")***";
Clovah answered 21/3, 2018 at 21:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.