简体   繁体   English

空C ++文件的用途是什么?

[英]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. 翻译阶段2的第二部分( N3485中的第2.2.2 )基本上说如果源文件没有以换行符结尾,编译器应该将其视为原样。

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. 我希望有人可以说明这一要求背后的原因。

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. 这是为了特别支持1994年在“最严重滥用规则”类别的国际混淆C代码竞赛中获胜: 世界上最小的自我复制计划。 Guaranteed. 保证。

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. 一个空的C ++源文件并不是特别有用 ,但禁止它是没有意义的。 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). 引用的子句不是要区分空文件和仅由一个新行组成的文件(它们之间应该没有真正的区别)。

我想这意味着每行都以\\ n结尾,而空文件没有行

预处理器可用于构建除程序源之外的东西,并且空白行可能很重要 - 例如,它通常用于分隔文本中的段落。

"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." “一个非空的源文件,不以新行字符结尾,或者在任何此类拼接发生之前以反斜杠字符开头的新行字符结尾处理的源文件,应该像另外一个新文件一样处理-line字符被附加到文件中。“

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. 翻译阶段2的第二部分(N3485中的第2.2.2节)基本上说如果源文件没有以换行符结尾,编译器应该将其视为原样。

No - it says that if the file "is not empty" AND does not end in a newline, then a newline is added 否 - 它表示如果文件“不为空”并且不以换行符结尾,则添加换行符

However, if I'm reading it correctly it makes an explicit exception for empty source files, which remain empty. 但是,如果我正确读取它,它会为空源文件做一个显式异常,它们仍为空。

Agreed. 同意。

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. 我希望有人可以说明这一要求背后的原因。

Consider a header file called "header.h" with last line as below with no trailing newline : 考虑一个名为“header.h”的头文件,最后一行如下,没有尾随换行符

#endif  // #ifndef INCLUDED_HEADER_H

Say another.cc includes it as follows: 说another.cc包含如下:

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

When another.cc is parsed, the text from header.h is substituted for the line specifying its inclusion. 解析another.cc时,header.h中的文本将替换指定其包含的行。 Done naively, that would result in: 天真地做,这将导致:

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

Obvious, the compiler would then fail to act on #include "another.h" , considering it part of the comment begun in header.h. 很明显,编译器将无法对#include "another.h" ,因为它是在header.h中开始的注释的一部分。

So, the rule for incomplete rules avoids these problems (which could be terribly hard to spot). 因此,不完整规则的规则避免了这些问题(这可能非常难以发现)。

If the file was empty anyway, this problem doesn't manifest: there's nothing like the #endif to be prepended to the next line in the including file.... 如果文件仍然是空的,那么这个问题就不会显现出来了:没有像#endif那样被包含在包含文件中的下一行....

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM