2012-06-15 CPlusPlus11

Raw String Literals

A raw string is a character sequence where there are no special characters. What you see is what you get. This makes it much easier to defined a string that contains a character sequence that would be interpreted as a special char if a normal string would have been used. Sequences like \n, \t, \b etc needs special treatment if not to be interpreted as special characters.

The syntax in its most straight forward form is this:


R for raw and "( and )" as starting and ending sequence allowing characters between be just what they are. Here are some examples of raw strings:

R"(hello "world")"               -> hello "world"
R"(no newline \n or tab \t)"     -> no newline \n or tab \t

Multiline "string"
          is just as it is typed
)"                               -> Multiline "string"
                                              is just as it is typed

As seen in the last example you can easily define large multilined text this way.

Another situation where raw strings pay off is when there are lots of chars that normally needs to be escaped. This is the situation with regular expressions where the back slash character, \, is used either to give or remove special meaning to characters. So in regular expressions defined by normal strings the back slash character itself needs to be escaped. We don't need all this extra escaping when using raw strings.

Regexp support isn't in gcc yet so I'll come back with examples later.

To complete the definition of raw strings. What if you need the terminating sequence )" in your raw string? The general definition of a raw string, which allows you to specify a delimiter sequence, solves this:


Where delimiter is an up to 16 char sequence with no whitespace. So you can write like this using # as delimeter:

R"#(')"')#" -> ')"'