Topic: Single-line comment and line continuation
Author: James Kanze US/ESC 60/3/141 #40763 <kanze@lts.sel.alcatel.de>
Date: 1996/03/06 Raw View
In article <4hgbpd$1pl@cnn.Princeton.EDU> tim@franck.Princeton.EDU
(Tim Hollebeek) writes:
|> Steve Clamage (clamage@Eng.Sun.COM) wrote:
|> : >I just ran across something in my C++ compiler dealing with single line
|> : >comments and the line continuation token:
|> : >
|> : > if ((ch[0] == '\\') && (ch[1] == '\\')) //look for initial \\
|> : >
|> : >This, under my compiler implementation will choke because of the line
|> : >continuation token, '\' at the end of the comment.
|> : In C++, simply don't use '//' comments on any line that ends with an
|> : escaped newline (including macro definitions!). In your example, it would
|> : make the comment more readable IMHO if you put the '\\' in quotes anyway,
|> : and would eliminate the problem.
|> Or simply add a single space after the last \, and take advantage of
|> that annoying behavior for once :-)
This will probably work on most Unix implementations, but it is not
guaranteed. An implementation is allowed to ignore trailing
whitespace when reading in the file. (This is to facilitate coding in
environments with fixed length records, where the system always pads
your line of code with blanks to make it the fixed length.)
--
James Kanze Tel.: (+33) 88 14 49 00 email: kanze@gabi-soft.fr
GABI Software, Sarl., 8 rue des Francs-Bourgeois, F-67000 Strasbourg, France
Conseils, itudes et rialisations en logiciel orienti objet --
-- A la recherche d'une activiti dans une region francophone
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]
Author: clamage@Eng.Sun.COM (Steve Clamage)
Date: 1996/03/04 Raw View
In article 331A@strata3d.com, Bret Pehrson <BretP@strata3d.com> writes:
>I just ran across something in my C++ compiler dealing with single line
>comments and the line continuation token:
>
> if ((ch[0] == '\\') && (ch[1] == '\\')) //look for initial \\
> {
> ...
>
>This, under my compiler implementation will choke because of the line
>continuation token, '\' at the end of the comment.
>
>Can someone please look in their copy of the current C++ 'standard' and tell
>me if this is correct?
Yes, it is a consequence of the phases of translation. Escaped newlines
are handled by pasting lines together before comments are removed, the
same as in C. (When C adds '//' comments, I expect it to have the same
behavior.)
The phases of translation were very carefully worked out by the ANSI C
committee for the C standard, and any change causes various things to break
or otherwise behave strangely. The fragility is due to interactions among
trigraphs
escaped newlines
escaped characters in strings or character constants
macro expansion.
In C++, simply don't use '//' comments on any line that ends with an
escaped newline (including macro definitions!). In your example, it would
make the comment more readable IMHO if you put the '\\' in quotes anyway,
and would eliminate the problem.
---
Steve Clamage, stephen.clamage@eng.sun.com
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]