Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ single line comments followed by \ transforms in multiline comment

Tags:

c++

c

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
like image 952
Emil Condrea Avatar asked Aug 06 '14 10:08

Emil Condrea


People also ask

Can one C comment include multiple lines?

You can comment out one or more lines of code in any C/C++ editor view. The leading characters // are added to the beginning of each line when commenting one or more lines of code.

What is the difference between single line and multiline comments?

There is no difference. It comes down to user preference. However, special variations on those syntaxes are interpreted by Javadoc for generating documentation.


4 Answers

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).

like image 142
Wojtek Surowka Avatar answered Sep 17 '22 19:09

Wojtek Surowka


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.

like image 36
James Kanze Avatar answered Sep 20 '22 19:09

James Kanze


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.

like image 24
Mohit Jain Avatar answered Sep 20 '22 19:09

Mohit Jain


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.

like image 27
cerkiewny Avatar answered Sep 18 '22 19:09

cerkiewny