Geoff Chappell - Software Analyst
single-line comment contains line-continuation character
This is a level 1 warning.
A single-line comment is the form of comment that is introduced by two forward slashes. The line-continuation character is the backslash, when occurring at the end of a line. The interaction between the two is that what counts as the single line for the comment may in fact be multiple lines of text, because line continuations are resolved before comments. For example,
int x; // this single-line comment \ continues to a second line \ and even a third
conforms perfectly with Microsoft’s documentation of the language. (If deduction from the Phases of Translation is too obscure, then look at C++ Comments for the explicit specification that single-line comments are terminated by “a new line not immediately preceded by a backslash.”) Yet, with only a few exceptions, Microsoft treats each continuation of a single-line comment to warning C4010. The fragment above produces the warning twice, at the second line and again at the third.
To escape warning C4010 in normal compilation, the line that a single-line comment continues to must either
For example,
int x; // this single-line comment contains a \ // and continues to a second line
produces no warning, and neither does
int x; // this single-line comment ends with a \ int y;
Note that examination of characters for the meaning of “white space” and “two forward slashes” allows for more line continuation, even between the two forward slashes. For example, the rather silly
int x; // this single-line comment contains a \ \ /\ / and continues to a second line
produces no warning, since the white space that begins on the second line is deemed to continue up to but not including the forward slash on the third, which is in turn deemed to be followed immediately by the forward slash at the beginning of the fourth line.
It may be as well to note explicitly that warning C4010 is not issued when the comment is merely being echoed as preprocessor output, i.e., when the /E, /EP or /P options are active in combination with /C and the comment is not in a #pragma directive.
The thinking behind this warning presumably starts from the problem that single-line comments present when the programmer wants that the text of the comment should end with a backslash that is actually just a backslash. The following example is at least plausible:
char *ProductDir; // fully-qualified path, with trailing \ char *ProductSettings; // filename, relative to ProductDir // C4010
but the programmer has not terminated the single-line comment, e.g., by following with an empty line, and what is intended as code on the second line is instead hidden in the first line’s comment.
To miss some code because the programmer has overlooked an implication of single-line comments is arguably worth a severe warning, but against this is that the warning is raised on only a suspicion. The slight variation
char *ProductDir; // fully-qualified path, with trailing \ backslash, all in lower case // C4010 char *ProductSettings; // filename, relative to ProductDir
misses nothing meaningful, and arguably does not deserve any warning, let alone at level 1.
A practical effect is that Microsoft has made an explicit feature of the C++ language unusable for many, if not most, programmers. To continue single-line comments over multiple lines by use of the backslash as a line-continuation character, yet compile cleanly, the programmer must disable (or demote) warning C4010. Many would be at least a little unsettled at the thought of disabling a level 1 warning as their routine practice. Indeed, some may even be prohibited from doing so, e.g., by an employer’s coding guide.