gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 15:22:25 24 4
gpt4 key购买 nike

我指的是:Why should text files end with a newline?其中一个答案引用了 C89 标准。简而言之,文件必须以新行结尾,新行前面不能紧跟反斜杠。

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

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

关于C++ 标准(源文件的换行符结尾),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31574837/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com