How to make C++ raw string which includes raw string terminator?

I use R"(...)" to define a raw string but, if the string actually contains a raw string terminator )", the compiler will complain.

For example:

auto tag = R"("(add)")"; // try to get string <"(add)">

How can I modify this so it works?

1 answer

  • answered 2018-04-17 04:48 StoryTeller

    The syntax for a raw string literal is something like the following:

    R"<delim>(...)<delim>"
    

    The optional delimiter that is next to the parentheses is there exactly for the reason you just stumbled upon. It's to let you include the raw string's control characters inside the literal. So add a delimiter:

    auto tag = R"tag("("add")")tag";