Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the utility of an empty C++ file?

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

However, if I'm reading it correctly it makes an explicit exception for empty source files, which remain empty.

The exact text (with added emphasis) is:

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.

I haven't been able to figure out any situations in which it would make a difference whether a source file was empty or consisted of only a newline character.

I'm hoping someone can shed some light on the reasoning behind this requirement.

like image 931
Samuel Edwin Ward Avatar asked Feb 19 '13 23:02

Samuel Edwin Ward


2 Answers

This is to specifically support the 1994 winning entry in the international obfuscated C code contest in the category "worst abuse of rules": The world's smallest self-replicating program. Guaranteed.

like image 114
edgar.holleis Avatar answered Sep 23 '22 05:09

edgar.holleis


I think the idea is that a source file normally consists of zero or more lines, and each line consists of a sequence of non-new-line characters followed by a new-line. Any source file not meeting that requirement needs special handling (so you don't get lines composed of text from two different source files).

An empty C++ source file is not particularly useful, but there's no point in forbidding it. The quoted clause isn't about distinguishing between an empty file and a file consisting of just one new-line (there should be no real difference between them).

like image 30
Keith Thompson Avatar answered Sep 24 '22 05:09

Keith Thompson