c++ - 在//C++ 注释中使用\\是否合法? (C++ 注释中的 LaTeX 方程)

标签 c++ g++ language-lawyer clang++

出于文档目的,我需要在我的 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/

相关文章:

c++ - POD 类型的二进制 I/O 如何不破坏别名规则?

c++ - 更改 ListView 和 TreeView 颜色的最简单方法

c++ - 如何在 MacOS X 上安装 g++?

c++ - 编译器不知道我的包含文件

c++ - 为什么 vector 的 move ctor 不推导出 noexcept()?

c++ - 如果标准指定它有一个析构函数,类应该不是可简单破坏的吗?

c++ - 开关盒中的变量 : UB or compiler bug?

java - 通过套接字传输文件时文件损坏

c++ - 如何使用 boost.log 写入文件?

c++ - shuffle() 函数和 SIMD 代码生成