escape R“()” in a raw string in C++

2020-02-10 05:50发布

  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?

2条回答
Emotional °昔
2楼-- · 2020-02-10 05:59

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)")~";
查看更多
戒情不戒烟
3楼-- · 2020-02-10 06:22

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

string raw_str = R"***(R"(foo)")***";
查看更多
登录 后发表回答