Search code examples
c++c++11delimiterstring-literalsrawstring

Include )" in raw string literal without terminating said literal


The two characters )" terminate the raw string literal in the example below.
The sequence )" could appear in my text at some point, and I want the string to continue even if this sequence is found within it.

R"(  
    Some Text)"  
)";       // ^^

How can I include the sequence )" within the string literal without terminating it?


Solution

  • Raw string literals let you specify an almost arbitrary* delimiter:

    //choose ### as the delimiter so only )###" ends the string
    R"###(  
        Some Text)"  
    )###";  
    

    *The exact rules are: "any member of the basic source character set except: space, the left parenthesis (, the right parenthesis ), the backslash \, and the control characters representing horizontal tab, vertical tab, form feed, and newline" (N3936 §2.14.5 [lex.string] grammar) and "at most 16 characters" (§2.14.5/2)