我一直在阅读 ISO 14882:2003。它说:
s-char:
any member of the source character set except the double-quote ", backslash \, or new-line character escape-sequence
universal-character-name
现在,关于换行符,当行结尾是 '\r' 时,我看到了一个问题
我写了一个小的cpp程序:
#include <fstream>
#include <string>
int main()
{
const char* program=""
"#include <string>\n"
"int main()\n"
"{\n"
" std::string s;\n"
" //s=\"\r"
" //\r"
" //\r"
" //\r"
" //\";\n"
" s=\"\\xAE\\xfffactory\\xAE\\xffaction\";\n"
" return 0;\n"
"}\n"
;
std::ofstream file("file.cpp", std::ios_base::trunc);
file << program;
file.close();
return 0;
}
在 Windows 上,file.cpp(在 VS 编辑器中读取)是:
#include <string>
int main()
{
std::string s;
//s="
//
//
//
//";
s="\xAE\xfffactory\xAE\xffaction";
return 0;
}
在编译 file.cpp 时,VS 在第 6 行而不是第 10 行触发和错误。
在 Linux 上,file.cpp(在 emacs 中读取)是:
#include <string>
int main()
{
std::string s;
//s="^M //^M //^M //^M //";
s="\xAE\xfffactory\xAE\xffaction";
return 0;
}
用 gcc 编译 file.cpp 我在第 10 行出现错误,而不是在第 6 行。
我应该从中得出什么结论?
最佳答案
你应该得出结论:
- VS 编辑器理解任何行尾,因此将其显示为多行(好吧,这是一个已知功能)。
- MSVC 编译器不理解
\r
行尾,所以它实际上将";
行计为第 6 行。 emacs
不理解\r
行尾(至少在默认情况下),因此它会在一行中显示源代码。- GCC 理解任何行结尾,因此它不会丢失计数。
啊,你从标准中提供的报价也是无关的。那里的换行符指的是源字符集,不是字符串中的\r
和\n
。您引用的语法规则仅排除字符串文字,例如:
const char* s = "some text, here comes 'new-line'
ha ha ";
关于c++ - 回车作为以c++文件结尾的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8021484/