45

Where is it documented in the C++ Standard the feature that if a line is commented using //some comment\ style (at the end of the line puts \) the comment is transformed to multiline?

Tested with g++ 4.8 and VS 2012

//some interesting stuff\
another interesting stuff\
etc
Emil Condrea
  • 9,705
  • 7
  • 33
  • 52
  • 2
    possible duplicate of [Whitespace character after backslash in C and C++](http://stackoverflow.com/questions/12305240/whitespace-character-after-backslash-in-c-and-c) – Joshua Taylor Aug 06 '14 at 22:22
  • 6
    @JoshuaTaylor How is that a duplicate? The linked question asks about **whitespace after backslash** while this question asks about the backslash only. – Alvin Wong Aug 07 '14 at 02:57
  • @AlvinWong, I don't know, but apparently he got someone else to agree with him. – Paul Draper Aug 07 '14 at 04:12
  • @JoshuaTaylor after 2 years of SO how comes that you don't know what is a duplicate question? When an answer from a question contains parts that are useful to another question it does not mean that the question is duplicate. – Emil Condrea Aug 07 '14 at 04:56
  • @AlvinWong The newline character *is* considered a whitespace character in C, so a question that asks how whitespace after a backslash is handled in C is more general than this one, and should include how one particular whitespace is handled in C. – Joshua Taylor Aug 07 '14 at 10:36
  • @EmilCondrea As I mentioned to Alvin, newlines *are* whitespace characters, so a question that asks how whitespace after slashes in C are handled *should* contain an answer to the more specific question about one particular whitespace character. As a more specific example, I think it'd make sense to at least consider "How is a newline after slash handled?" as a possible duplicate of "How are newlines and tabs after slash handled?" That's a nice thing about the voting process here; each voter can identify *possible* duplicates, but others still need to agree. – Joshua Taylor Aug 07 '14 at 10:41
  • @PaulDraper The *possible* duplicate is about whitespace after backslash. In C, the newline character is one of a number of whitespace characters. A question that asks "how are whitespace characters after a slash handled?" *should* contain an answer to "how is this one particular whitespace character after a slash handled?" – Joshua Taylor Aug 07 '14 at 10:43
  • @JoshuaTaylor You seem to have misunderstood both questions. In this case, a newline is different from a whitespace. Read the answers below. – Alvin Wong Aug 07 '14 at 10:45
  • @AlvinWong I understand that all whitespace is not handled the same, and that slash+newline has a meaning and usage, whereas slash+other_whitespace doesn't. My point is that an answer to "slash + whitespace" (the more general case) should cover both cases, explaining that "slash + whitespace" doesn't mean anything in general, but that the specific case of "slash + newline" does. – Joshua Taylor Aug 07 '14 at 10:47
  • @JoshuaTaylor The linked question asks specificly about a whitespace (excluding newline). Try highlighting the code and you will see it. – Alvin Wong Aug 07 '14 at 10:48
  • @AlvinWong The code *example* in the other question uses a space after the slash; I was aware of that when I voted. The actual question, though, asks about "whitespace character (or several characters) after backslash?" A given question can (usually) be exemplified by lots of different code snippets. That one just happened to use "slash/space/newline". Another could use "slash/tab/space/carriage-return/newline". The important thing is that the question *asks* about the behavior of one or more whitespace characters after a slash. – Joshua Taylor Aug 07 '14 at 10:54
  • @JoshuaTaylor The other question says "Does it guarantees to join lines anyway or not?" so the intentions are clear - the question is about whitespace characters different than newline. – Wojtek Surowka Aug 07 '14 at 10:56
  • 1
    @JoshuaTaylor My interpretation is that the linked question asks whether the backslash line continuation still works **when having an extra whitespace** before the newline, which is a different question. – Alvin Wong Aug 07 '14 at 10:56

6 Answers6

93

C++ standard, 2.2 - phases of translation. Phase 2 includes

Each instance of a backslash character (\) immediately followed by a new-line character is deleted, splicing physical source lines to form logical source lines.

and Phase 3 includes

Each comment is replaced by one space character

So the backslash at the end of the line is recognised before comments.

Equivalent phases 2 and 3 for C can be found in C standard (5.1.1.2 Translation phases in my draft).

David G
  • 94,763
  • 41
  • 167
  • 253
Wojtek Surowka
  • 20,535
  • 4
  • 44
  • 51
  • 6
    Who on earth down-voted this answer? It's the most complete and correct (better than mine, for example). – James Kanze Aug 06 '14 at 10:14
  • 1
    How can this be useful if not confusing? – Ray Aug 06 '14 at 16:45
  • 5
    @DebugErr The *intent* was that on a system that imposed a hard upper limit on the length of physical source lines (e.g. punchcards) you could mechanically split every too-long line with \-newlines without having to parse the code. I doubt this scenario has ever occurred in real life, but that was the original rationale. – zwol Aug 06 '14 at 17:02
  • 6
    backslash-newline is often used in long macro definitions. – Alex Jasmin Aug 06 '14 at 22:05
  • 3
    Keep in mind that `//`-comments are relatively new to C-family languages, and in particular are *much* newer than the preprocessor. As with anything that evolves, there's conflicts that must be resolved, and the resolution isn't always the most elegant. – fluffy Aug 07 '14 at 10:15
12

A \ followed by a new line is eliminated very early in the translation process, before the compiler starts looking for comments and the end of comments, see §2.2, Phases of translation.

James Kanze
  • 150,581
  • 18
  • 184
  • 329
11

Do you want to know for C or C++? (EDIT: In original question OP asked for C/C++)

For C following section from ISO/IEC 9899:TC2 Committee Draft — May 6, 2005 WG14/N1124 answers your question.

5.1.1.2 Translation phases

[2] Each instance of a backslash character () immediately followed by a new-line character is deleted, splicing physical source lines to form logical source lines. Only the last backslash on any physical source line shall be eligible for being part of such a splice. A source file that is not empty shall end in a new-line character, which shall not be immediately preceded by a backslash character before any such splicing takes place.

For C++, you can refer to Phase 2 at en.cppreference.com

1) Whenever backslash appears at the end of a line (immediately followed by the newline character), both backslash and newline are deleted, combining two physical source lines into one logical source line. This is a single-pass operation, a line ending in two backslashes followed by an empty line does not combine three lines into one). If a universal character name (\uXXX) is formed on this phase, the behavior is undefined.
2) If a non-empty source file does not end with a newline character after this step (whether it had no newline originally, or it ended with a backslash) the behavior is undefined (until C++11) a terminating newline character is added (since C++11)

If your current line is a single line comment, following line would be digested in continuation as a comment.

Mohit Jain
  • 30,259
  • 8
  • 73
  • 100
7

http://www.cplusplus.com/forum/general/33653/

You can add the "\" anywhere in the code and newline will be ignored.

As a better reference the 2.2 paragraph of the standard:

Each instance of a backslash character () immediately followed by a new-line character is deleted, splicing physical source lines to form logical source lines. Only the last backslash on any physical source line shall be eligible for being part of such a splice. If, as a result, a character sequence that matches the syntax of a universal-character-name is produced, the behavior is undefined. A source file that is not empty and that does not end in a new-line character, or that ends in a new-line character immediately preceded by a backslash character before any such splicing takes place, shall be processed as if an additional new-line character were appended to the file.

This is not clear regarding what happens if the last character in the file is a backslash. In such a case, presumably the result of adding the newline should not be a line splice but rather a backslash preprocessing-token (that will be diagnosed as an invalid token in phase 7), but that should be spelled out.

Jim Balter
  • 16,163
  • 3
  • 43
  • 66
cerkiewny
  • 2,761
  • 18
  • 36
  • 3
    The reference you cite isn't very good, and doesn't really explain what is happening from the standard point of view. – James Kanze Aug 06 '14 at 10:13
  • Attached the standard paragraph as the better reference as well, but I can see that others already did it. – cerkiewny Aug 06 '14 at 10:17
  • "a backslash preprocessing-token (that will be diagnosed as an invalid token in phase 7)" -- no, a preprocessing token cannot produce a diagnostic if it appears as part of a comment that is removed before phase 7. A trailing backslash is harmless (and isn't a separate preprocessing token). – Jim Balter Aug 06 '14 at 10:24
5

As per Working Draft, Standard for Programming Language C++, Chapter 2 Lexical conventions, 2.1 - 2) :

Each instance of a new-line character and an immediately preceding backslash character a backslash character () immediately followed by a new-line character is deleted, splicing physical source lines to form logical source lines. Only the last backslash on any physical source line shall be eligible for being part of such a splice.

This is also applicable to comments and this is still part of the final version.

Shlublu
  • 10,917
  • 4
  • 51
  • 70
-6

It's in the C++ Standard, like everything about the C++ language. You can download a draft of the C++ Standard for free (for almost everybody except people involved in the design of the C++ language and compiler writers the draft is good enough), just google for "C++ Standard Draft".

gnasher729
  • 51,477
  • 5
  • 75
  • 98
  • It is not answering the question. The reason OP added question is he couldn't find the information in the standard. – cerkiewny Aug 06 '14 at 10:18
  • @cerkiewny, that's an assumption. Nowhere in the post does it explicitly say they couldn't find the answer. They, perhaps, don't care to look; that's not clear. – ChiefTwoPencils Aug 06 '14 at 10:20
  • 2
    Take a look at this post: http://meta.stackexchange.com/questions/15650/ban-lmgtfy-let-me-google-that-for-you-links it explains why your approach of answering the question shouldn't really be used. – cerkiewny Aug 06 '14 at 10:22
  • 3
    ...however, this is not an answer to the question. op specifically asked for the location *in* the standard. It's not as if searching the standard for a clarification is always willy-nilly. – ChiefTwoPencils Aug 06 '14 at 10:24