Namespaces
Variants
Actions

Talk:cpp/language/translation phases

From cppreference.com

According to my understanding of the "Phases of Translation" as outlined here the following program

   #include <iostream>
   #define greet R"(\
   hi!\
   )"
   int main() {
       std::cout << greet;
   }

should simply print `hi!´ (the three letters without newline). I think there is little room for interpreting this differently:

Phase 2 1) Whenever backslash appears at the end of a line (immediately followed by the newline character), both backslash and newline are deleted, ...

But testing with g++ (4.9), clang++ (3.5), and recent VC++ (as available here: http://rextester.com/runcode) gives two lines of output (first just a backslash, then `hi!´ followed by a backslash).

As cppreference.com is otherwise an excellent and usually very precise source for what the C++ standard mandates, I'm still inclined to believe it is right.

OTH, as three different compilers agree in their (different) interpretation, all three must implement this wrong in the same way ...?

Mwe (talk) 04:27, 18 May 2015 (PDT)


N4140 §2.5 [lex.pptoken]p3 says:
If the input stream has been parsed into preprocessing tokens up to a given character:
  • 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 (trigraphs, universal-character-names, and line splicing) are reverted; this reversion shall apply before any d-char, r-char, or delimiting parenthesis is identified.
In your example,
 R"(\
    hi!\
    )"

is a raw string literal, thus transformations performed in phases 1 and 2 are reverted. The compilers do nothing wrong here. --D41D8CD98F (talk) 05:59, 18 May 2015 (PDT)

I added a brief note to stage 3 description on this page. Good to hear major compilers agree here (they don't agree on how simple line splicing works, GCC bug #8270) --Cubbi (talk) 06:36, 18 May 2015 (PDT)

Thanks for adding the clarification.

Should I consider to add my original example as compilable example to the page? Mwe (talk) 07:03, 18 May 2015 (PDT)