C ++,用多行代码注释

时间:2018-08-17 13:36:47

标签: c++ gcc clang comments

该代码不是:

int Func(int a, // comment
         int b, // comment
         int c  // comment
        ) ...

等同于:

int Func(int a, // comment int b, // comment int c  // comment) ...

为什么它正确构建(至少在G ++中)?

到目前为止,在这种情况下,我总是使用/* */条注释。

4 个答案:

答案 0 :(得分:6)

int Func(int a, // comment
         int b, // comment
         int c  // comment
        ) ...

已转换为

int Func(int a,  
         int b,  
         int c   
        ) ...
third phase of translation期间

。如果您希望在翻译阶段开始之前等一行,那么您需要使用/* */,例如

    int Func(int a /*comment 1*/, int b /*comment 2*/, int c /*comment 3*/ ) ...

答案 1 :(得分:4)

standard

  

5.7条评论

     
    

字符//开始注释,该注释立即终止     在下一个换行符之前...

  

因此,在去除注释之后,编译器最终解释的代码如下:

int Func(int a,
         int b,
         int c 
        ) {}

即使换行符也将保持不变。

答案 2 :(得分:1)

注释与代码无关,因此此代码:

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) {}

//开头的单行注释在该行的结尾处结束,因此将代码与注释放在同一行上会将代码转换为注释,并且您的两个摘要不相同。

答案 3 :(得分:0)

有两种评论:

  • //开头的注释在行尾终止
  • /*开头的评论由下一个*/终止

最初的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 */) ...