Home > front end >  Why is raw string literal parsed before trailing backslash?
Why is raw string literal parsed before trailing backslash?

Time:12-26

From Phases of translation, backslash joining next line happens in Phase 2 and string literal evaluation happens in Phase 3. Then why does the following code does string evaluation before?

#include<string>
#include<iostream>

int main() {
    std::string s = R"(before\
after)";
    std::cout << s;
}

gives:

before\
after

instead of:

beforeafter

Phase 2

  1. Whenever backslash appears at the end of a line (immediately followed by zero or more whitespace characters other than new-line followed by (since C 23) the newline character), these characters are deleted, combining two physical source lines into one logical source line. [...]

Phase 3

  1. The source file is decomposed into comments, sequences of whitespace characters (space, horizontal tab, new-line, vertical tab, and form-feed), and preprocessing tokens, which are the following: a) header names such as or "myfile.h" b) identifiers c) preprocessing numbers d) character and string literals , including user-defined (since C 11) [...]

CodePudding user response:

Raw string literals explicitly undo phases 1&2:

If the next character begins a sequence of characters that could be the prefix and initial double quote of a raw string literal, such as R", the next preprocessing token shall be a raw string literal. Between the initial and final double quote characters of the raw string, any transformations performed in phases 1 and 2 (universal-character-names and line splicing) are reverted; this reversion shall apply before any d-char, r-char, or delimiting parenthesis is identified.

  • Related