C++ Multi-line comments using backslash

2019-01-14 07:37发布

问题:

Can // style comments be continued to the next line by using a back slash, like multi-line macros? E.g.

// here is a comment \
   and this is more comments \
const char* x = "hello";  // this line of "code" is actually still a comment
int x = 5; // and now an actual line of code

回答1:

Yes. Lines terminated by a \ are spliced together with the next line very early in the process of translation. It happens at phase 2 of translation, before comment removal and before preprocessor has a chance to do its work.

Comment recognition and removal takes place at phase 3. For this reason you can turn a // comment into what looks like a multi-line comment by using the \. This usually fools most syntax-highlighting source code parsers.

Preprocessor works at phase 4.

This all means that you can "multiline" virtually anything using the \, including comments and preprocessor directives

#\
d\
e\
f\
i\
n\
e \
ABC \
int i

int main() {
A\
B\
C = 5;
}

P.S. Please note that the terminating \ does not introduce any whitespace into the spliced line. This should be taking onto account when writing multi-line comments using the \ feature. For example, the following comment

// to\
get\
her

stands for the single word "together" and not for three separate words "to get her". Obviously, incorrect use of \ in comments might drastically obfuscate and even distort their intended meaning.



回答2:

Here's an excellent reason not to do this. The following program prints "This will appear".

#include <iostream>
int main()
{
    std::cout << "This "
    // A comment ... \ 
    << "will appear"
    // Another comment ... \
    << ", but this won't"
    << std::endl;
}

Why? Because the first \ is followed by a blank, and so it's just part of the comment, not a line-splicing character. The program's behavior can quietly and significantly change due to invisible trailing white space.

An even better reason not to do this: g++ gets it wrong, even with -pedantic. When I compile this program using g++ the output is just "This"; the trailing white space after the first \ is ignored. In my opinion this is how it should work, but it's not what the language standard says. (Line splicing happens in translation phase 2. I suppose one might argue that the trailing blanks could be deleted in phase 1, but I'm not convinced that that's a valid argument -- and I don't know whether the gcc authors have actually made that argument.) In any case, g++ 4.5.2 and Sun CC version 5.5 disagree with each other.

If you want multi-line comments, either use /* ... */, or insert a // at the beginning of each line. I prefer the latter, because it's much easier to tell that a given line is part of the comment. (Actually it's multiple single-line comments.) Any decent editor should let you do this without typing // N times for N lines. Or, if you're commenting out a block of code, use #if 0 ... #endif.



回答3:

I was going to say no, but it looks like it actually (according to vim's syntax highlighting).



回答4:

I originally said "No, you can't".

But I still say "No, you SHOULDN'T"!

Use /* */ instead.

EXAMPLE:

#include <stdio.h>

int main ()
{
  // Begin comment \
     continue comment?
  return printf ("Hello world!\n");
}

The above compiles, for reasons explained in other posts. But it's WRONG:

  • "//" are INTENDED for single-line comments (you should use "/* */" for nested, and for multi-line comments

  • Relying on "\" continuation lines is a great way to inject weird, difficult to debug errors in your program.

Just like eating stringbeans through your nose: even if you CAN, you probably SHOULDN'T.

IMHO...



标签: c++ comments