出于文档目的,我需要在我的 C++ 注释中添加一些 LaTeX 方程,例如:
//
// \begin{eqnarray*}
// y_1 &=& x_1 \\
// y_2 &=& x_2
// \end{eqnarray*}
//
int main() {}
使用 clang++(版本 9.0.1-10)我可以毫无问题地编译我的代码:clang++ -Wall prog.cpp
但是使用 g++(版本 9.2.1)g++ -Wall prog.cpp
我收到此警告:prog.cpp:3:1: warning: multi-line comment [-Wcomment] 3 | // y_1 &=& x_1 \\ | ^
我的问题:哪个编译器是对的?我可以合法使用
\\
C++ 内部 //
评论?
最佳答案
这样的评论是合法的,但它们可能会产生意想不到的效果,因此发出警告。末尾带反斜杠的下一行是注释的延续,不管 //
一开始。所以这
// \\
Hey dude!
int main () {}
是一个有效的 C++ 程序。不,最后一个之前的反斜杠不能用作转义符。
如果您想避免出现警告,请在行尾添加 LaTeX 注释:
// y_1 &=& x_1 \\ % look ma, no warning
请注意,反斜杠和换行符之间的简单空格不一定能解决问题。 GCC 文档说:
If there is white space between a backslash and the end of a line, that is still a continued line. However, as this is usually the result of an editing mistake, and many compilers will not accept it as a continued line, GCC will warn you about it.
关于c++ - 在//C++ 注释中使用\\是否合法? (C++ 注释中的 LaTeX 方程),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62126465/