'What is the purpose of d-char-sequence in C++ raw strings?

On this reference: https://en.cppreference.com/w/cpp/language/string_literal, raw string is defined as:

 prefix(optional) R"d-char-sequence(optional)(r-char-sequence(optional))d-char-sequence(optional)"

Example:

const char* s1 = R"foo(
Hello
  World
)foo";

What is the purpose of the d-char-sequence ("foo" in the example above)?

c++


Solution 1:[1]

The optional d-char-sequence is used to define the end marker of the raw string.

For example, if the raw string contains the substring )", then the line:

const char* s = R"(string with )" inside)";

will raise a syntax error. This can be fixed by using the optional d-char-sequence that is not met inside the raw string:

const char* s = R"uniq(string with )" inside)uniq";

Solution 2:[2]

It lets you embed )" in the literal, which would otherwise be considered the end of the literal.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Remy Lebeau
Solution 2 HolyBlackCat