Search code examples
c++comments

Why can my comment consist of so much forward slash (/)?


I know that there are many types of comment, I will list out a few of them (those related):

  • // - Normal comment

  • /// - This would make the comment bold

And surprisingly, the IDE would not raise an error in this code (Even it is not executed, it should still control the programmer somewhere):

/////////////////// HI!

Why would the standard allow this to happen?

BTW, my IDE is Code::Blocks 20.03 if it matters.


Solution

  • As per the C++ standard: lex.comment

    The characters // start a comment, which terminates immediately before the next new-line character.

    From the above, you can infer that every character (other than newline) which follows the first two / characters is part of the comment.

    If that wasn't already clear enough, it goes on to note:

    The comment characters //, /*, and */ have no special meaning within a // comment and are treated just like other characters.