C++,在多行代码中注释
C++, comment in a multi-line piece of code
不应该代码:
int Func(int a, // comment
int b, // comment
int c // comment
) ...
相当于:
int Func(int a, // comment int b, // comment int c // comment) ...
为什么它可以正确构建(至少使用 G++)?
到目前为止,我总是在这种情况下使用 /* */
注释。
int Func(int a, // comment
int b, // comment
int c // comment
) ...
转换为
int Func(int a,
int b,
int c
) ...
在 third phase of translation 期间。如果你想在翻译阶段发生之前有一行等价的,那么你需要使用 /* */
like
int Func(int a /*comment 1*/, int b /*comment 2*/, int c /*comment 3*/ ) ...
注释与代码无关,因此这段代码:
int Func(int a, // comment
int b, // comment
int c // comment
) {}
实际上等同于:
int Func(int a,
int b,
int c
) {}
或者如果你还想要这个:
int Func(int a,int b,int c) {}
以 //
开头的单行注释在行尾结束,因此将代码与注释放在同一行会将代码变成注释,并且您的两个代码段不等价。
来自standard
5.7 Comments
The characters // start a comment, which terminates immediately
before the next new-line character ...
因此,去掉注释后,编译器最终解释出来的代码是这样的:
int Func(int a,
int b,
int c
) {}
即使是换行符也会保持不变。
有两种评论:
- 以
//
开头的注释在行尾终止
- 以
/*
开头的评论由下一个 */
终止
最初的 C 规范只知道 /* */
风格的注释。
//
风格的注释是在 C++ 中引入的,后来在 C 标准中也引入了(但不确定是哪一个)。
所以这个:
int Func(int a, // comment
int b, // comment
int c // comment
) ...
相当于:
int Func(int a, /* comment */
int b, /* comment */
int c /* comment */
) ...
相当于:
int Func(int a, /* comment */ int b, /* comment */ int c /* comment */) ...
不应该代码:
int Func(int a, // comment
int b, // comment
int c // comment
) ...
相当于:
int Func(int a, // comment int b, // comment int c // comment) ...
为什么它可以正确构建(至少使用 G++)?
到目前为止,我总是在这种情况下使用 /* */
注释。
int Func(int a, // comment
int b, // comment
int c // comment
) ...
转换为
int Func(int a,
int b,
int c
) ...
在 third phase of translation 期间。如果你想在翻译阶段发生之前有一行等价的,那么你需要使用 /* */
like
int Func(int a /*comment 1*/, int b /*comment 2*/, int c /*comment 3*/ ) ...
注释与代码无关,因此这段代码:
int Func(int a, // comment
int b, // comment
int c // comment
) {}
实际上等同于:
int Func(int a,
int b,
int c
) {}
或者如果你还想要这个:
int Func(int a,int b,int c) {}
以 //
开头的单行注释在行尾结束,因此将代码与注释放在同一行会将代码变成注释,并且您的两个代码段不等价。
来自standard
5.7 Comments
The characters // start a comment, which terminates immediately before the next new-line character ...
因此,去掉注释后,编译器最终解释出来的代码是这样的:
int Func(int a,
int b,
int c
) {}
即使是换行符也会保持不变。
有两种评论:
- 以
//
开头的注释在行尾终止 - 以
/*
开头的评论由下一个*/
终止
最初的 C 规范只知道 /* */
风格的注释。
//
风格的注释是在 C++ 中引入的,后来在 C 标准中也引入了(但不确定是哪一个)。
所以这个:
int Func(int a, // comment
int b, // comment
int c // comment
) ...
相当于:
int Func(int a, /* comment */
int b, /* comment */
int c /* comment */
) ...
相当于:
int Func(int a, /* comment */ int b, /* comment */ int c /* comment */) ...