I came across this code snippet in C++17 draft n4713:
#define R "x" const char* s = R"y"; // ill-formed raw string, not "x" "y"
What is a "raw string"? What does it do?
Python raw string is created by prefixing a string literal with 'r' or 'R'. Python raw string treats backslash (\) as a literal character. This is useful when we want to have a string that contains backslash and don't want it to be treated as an escape character.
In Python, strings prefixed with r or R , such as r'...' and r"..." , are called raw strings and treat backslashes \ as literal characters. Raw strings are useful when handling strings that use a lot of backslashes, such as Windows paths and regular expression patterns.
Raw String Literal in C++ A Literal is a constant variable whose value does not change during the lifetime of the program. Whereas, a raw string literal is a string in which the escape characters like ' \n, \t, or \” ' of C++ are not processed. Hence, a raw string literal that starts with R”( and ends in )”.
Unlike a regular string, a raw string treats the backslashes ( \ ) as literal characters. Raw strings are useful when you deal with strings that have many backslashes, for example, regular expressions or directory paths on Windows.
Raw string literals are string literals that are designed to make it easier to include nested characters like quotation marks and backslashes that normally have meanings as delimiters and escape sequence starts. They’re useful for, say, encoding text like HTML. For example, contrast
"<a href=\"file\">C:\\Program Files\\</a>"
which is a regular string literal, with
R"(<a href="file">C:\Program Files\</a>)"
which is a raw string literal. Here, the use of parentheses in addition to quotes allows C++ to distinguish a nested quotation mark from the quotation marks delimiting the string itself.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With