Why are nested comments forbidden in C++, Java inspite of the fact that nested comments are useful, neat, and elegant and can be used to comment out statements that have comments?
C and C++ do it for ease of parsing. This way, when they hit a comment start of /*, the parser can trivially scan to the end. Otherwise, it would have to set up and maintain a stack, and then report errors if the comment tokens are unmatched.
Multi-line comments are visible to Scheme as a single whitespace. Multi-line comments may be nested: Every #| within the comment starts a new multi-line comment, which has to be terminated by a matching |# .
A nested comment is a comment inside another comment.
C and C++ do it for ease of parsing. This way, when they hit a comment start of /*, the parser can trivially scan to the end. Otherwise, it would have to set up and maintain a stack, and then report errors if the comment tokens are unmatched.
As to why Java does it, the answer is simple - Java's syntax was designed to emulate C and C++. If nested comments were allowed, it might trip up some C programmers, and many angry blog posts would be written!
At least for C++ that's only partly true, there's no problem with:
/*
//
//
*/
However if you already got an /* comment in a section you want to comment out you might be able to do it by surrounding it with an #if 0
which I think many compilers optimize away. As in:
#if 0
/*
*/
#endif
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With