C ++标准(源文件的换行结束)

C++ Standards (newline ending of source files)

我指的是:为什么文本文件以换行符结尾?
其中一个答案引用了C89标准。 这简单地说明文件必须以新行结束,而新行不会立即以反斜杠开头。

这适用于最新的C ++标准吗?

1
2
3
4
5
6
7
8
9
#include <iostream>
using namespace std;

int main()
{
  cout <<"Hello World!" << endl;
  return 0;
}
//\

以上是否有效? (假设//后面有换行符,我无法显示)


给定的代码在C ++的情况下是合法的,但不适用于C.

实际上,C(N1570)标准说:

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.

C ++标准(N3797)有点不同(强调我的):

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.