What is an empty c ++ file utility?

The second part of translation phase 2 (section 2.2.2 in N3485 ) basically says that if the source file does not end with a newline, the compiler should treat it as if it were doing it.

However, if I read it correctly, it makes an explicit exception for empty source files that remain empty.

Exact text (with accent):

Each instance of the backslash character ( \ ) immediately followed by a newline character is deleted by splicing the physical source lines to form the logical lines of the source. Only the last backslash in any physical line of the source should be allowed to be part of such a splicing. If the result is a sequence of characters that matches the syntax of the name of the universal character, the behavior is undefined. A source file that is not empty and that does not end with a newline or ends with a newline that is preceded by a backslash before any such merging occurs is processed as if a new line character were added to the file .

I was not able to figure out any situations in which it would matter if the source file was empty or consisted only of a newline.

I hope that someone can shed light on the reasoning behind this requirement.

+6
source share
5 answers

I think the idea is that the source file usually consists of zero or more lines, and each line consists of a sequence of characters other than a new line, followed by a new line. Any source file that does not meet this requirement needs special processing (therefore, you do not get lines consisting of text from two different source files).

An empty C ++ source file is not particularly useful, but there is no point in banning it. The citation proposal is not to distinguish between an empty file and a file consisting of only one new line (there should be no real difference between them).

+2
source

This is specifically to support the 1994 entry in the international obfuscation C-code contest in the category of โ€œworst rule abuseโ€: The smallest self-reproduction program in the world. Guaranteed.

+5
source

I assume this means that each line ends with \ n and the empty file has no lines

+2
source

A preprocessor can be used to create objects in addition to the source of the program, and an empty line can be significant - often used to separate paragraphs in the text, for example.

+1
source

"A source file that is not empty and that does not end with a newline, or that ends with a newline, which is preceded by a backslash before any such merging occurs, is processed as if an additional character had been added to the file new line.

The second part of translation phase 2 (section 2.2.2 in N3485) basically says that if the source file does not end with a newline, the compiler should treat it as if it were doing it.

No - it says that if the file is "not empty" and does not end in a new line, a new line is added

However, if I read it correctly, it makes an explicit exception for empty source files that remain empty.

I agree.

I was not able to figure out any situations in which it would matter if the source file was empty or consisted only of a newline. I hope that someone can shed light on the reasoning behind this requirement.

Consider a header file called "header.h" with the last line, as shown below, without a trailing new line:

 #endif // #ifndef INCLUDED_HEADER_H 

Let's say another.cc includes the following:

 #include "header.h" #include "another.h" 

When analyzing another.cc, the text from header.h is replaced with a line indicating its inclusion. Done naively, which will lead to:

 #endif // #ifndef INCLUDED_HEADER_H#include "another.h" 

Obviously, the compiler will then be unable to act on #include "another.h" , considering it to be part of the comment started in header.h.

Thus, the rule for incomplete rules avoids these problems (which can be very difficult to detect).

If the file is still empty, this problem does not occur: nothing looks like #endif for the next line in the included file ....

0
source

All Articles