 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
bloodymir Guest
|
Posted: Wed Oct 05, 2005 9:33 pm Post subject: embedded '\' in comment |
|
|
Hi!
Recently I had a problem with an array initialization. After
quite some testing I found the compiler (gcc3) was missing a
line. I tracked down the problem to a "//" comment on the line
before which ends on double backslash. It works like this:
int main() {
int i = 0;
// \
++i;
cout << i;
}
So shall "++i" be part of the comment or not?
Frank
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Markus Moll Guest
|
Posted: Thu Oct 06, 2005 1:29 pm Post subject: Re: embedded '\' in comment |
|
|
Hi
bloodymir wrote:
| Quote: | int main() {
int i = 0;
// \
++i;
cout << i;
}
So shall "++i" be part of the comment or not?
|
Yes, your compiler is right.
Markus
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Alberto Ganesh Barbati Guest
|
Posted: Thu Oct 06, 2005 1:34 pm Post subject: Re: embedded '\' in comment |
|
|
bloodymir wrote:
| Quote: | Hi!
Recently I had a problem with an array initialization. After
quite some testing I found the compiler (gcc3) was missing a
line. I tracked down the problem to a "//" comment on the line
before which ends on double backslash. It works like this:
int main() {
int i = 0;
// \
++i;
cout << i;
}
So shall "++i" be part of the comment or not?
|
Yes, "++i" is part of the comment.
If a line (comment or not, doesn't matter) ends with a backslash then
the newline and the backslash are ignored in translation phase 2 and the
two "physical lines" are merged to form a single "logical line"
(§2.1/2). The presence of two backslashes in your example is an
accident, only one would suffice. In this particular case the two
physical lines:
------
// \
++i;
------
are interpreted as
------
// ++i;
------
(notice that the leading blanks of the second line are still kept in
this translation phase). This rules applies to comments also because
comments are removed during translation phase 3.
This feature may look misleading, but it's indeed useful to create
multi-line macros:
#define MULTI_LINE_MACRO(x)
if(x)
{
foo();
}
else
{}
HTH,
Ganesh
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Victor Bazarov Guest
|
Posted: Thu Oct 06, 2005 1:38 pm Post subject: Re: embedded '\' in comment |
|
|
bloodymir wrote:
| Quote: | Recently I had a problem with an array initialization. After
quite some testing I found the compiler (gcc3) was missing a
line. I tracked down the problem to a "//" comment on the line
before which ends on double backslash. It works like this:
int main() {
int i = 0;
// \
++i;
cout << i;
}
So shall "++i" be part of the comment or not?
|
Yes, it shall. Phase 2 of compilation removes the newline with
immediately preceding backslash, so after phase 2 your code should
look like this:
int main() {
int i = 0;
// ++i;
cout << i;
}
Phase 3 replaces each comment with a single space character.
You could just add a space after the second backslash...
V
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Jonathan Leffler Guest
|
Posted: Thu Oct 06, 2005 1:48 pm Post subject: Re: embedded '\' in comment |
|
|
bloodymir wrote:
| Quote: | Hi!
Recently I had a problem with an array initialization. After
quite some testing I found the compiler (gcc3) was missing a
line. I tracked down the problem to a "//" comment on the line
before which ends on double backslash. It works like this:
int main() {
int i = 0;
// \
++i;
cout << i;
}
So shall "++i" be part of the comment or not?
|
backslash-newline combinations are removed in practically the first
phase of pre-processing - so the comment is slash, slash, spaces, plus,
plus, i, semi-colon.
Put a space after the second backslash and you have one line of comment.
/
/ silly comment!
That's a comment too - comments are resolved after backslash-newline
removal.
/
* This is *
/
That's a C style comment. Nasty, isn't it...
--
Jonathan Leffler #include
Email: [email]jleffler (AT) earthlink (DOT) net[/email], [email]jleffler (AT) us (DOT) ibm.com[/email]
Guardian of DBD::Informix v2005.02 -- http://dbi.perl.org/
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Martin Bonner Guest
|
Posted: Thu Oct 06, 2005 3:10 pm Post subject: Re: embedded '\' in comment |
|
|
bloodymir wrote:
| Quote: | Hi!
Recently I had a problem with an array initialization. After
quite some testing I found the compiler (gcc3) was missing a
line. I tracked down the problem to a "//" comment on the line
before which ends on double backslash. It works like this:
int main() {
int i = 0;
// \
++i;
cout << i;
}
So shall "++i" be part of the comment or not?
|
The problem is not a DOUBLE backslash, it is a backslash at the very
end of a line.
Very early in the preprocessing (before #define is interpreted),
multiple physical lines are merged into a single logical line by
removing back-slash, end-of-line pairs. This is useful if you have a
complex macro that you want to split over multiple lines. For example
I have a macro to generate a forwarding function:
#define DO_ACTION_NO_ARGS( Function )
HRESULT Dispatcher::Function()
{
HRESULT hResult = m_pTarget->Function( m_Arg1, m_Arg2 );
if ( FAILED( hResult ) )
{
SetError( hResult, IDS_UNSUCCESSFUL, #Function );
}
return hResult;
}
Line splicing is less useful in your example. Unfortunately, it
happens BEFORE comments are removed. Thus the compiler sees:
int main() {
int i = 0;
// ++i;
cout << i;
}
It could be argued that line splicing should be applied AFTER comments
have been removed, but it is about two decades too late to have that
argument.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Yuri Khan Guest
|
Posted: Thu Oct 06, 2005 3:11 pm Post subject: Re: embedded '\' in comment |
|
|
bloodymir wrote:
| Quote: | Hi!
Recently I had a problem with an array initialization. After
quite some testing I found the compiler (gcc3) was missing a
line. I tracked down the problem to a "//" comment on the line
before which ends on double backslash. It works like this:
int main() {
int i = 0;
// \
++i;
cout << i;
}
So shall "++i" be part of the comment or not?
|
On the second phase of translation (2.1/1), which is pretty early, all
instances of backslash followed by newline are removed. This is called
line splicing and widely used when defining multiline macros. The fact
that the backslash is doubled is irrelevant.
Thus, "++i;" shall be part of the comment. A good source editor with
syntax highlighting will display it as such.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Tim Van Holder Guest
|
Posted: Thu Oct 06, 2005 3:14 pm Post subject: Re: embedded '\' in comment |
|
|
bloodymir wrote:
| Quote: | Hi!
Recently I had a problem with an array initialization. After
quite some testing I found the compiler (gcc3) was missing a
line. I tracked down the problem to a "//" comment on the line
before which ends on double backslash. It works like this:
int main() {
int i = 0;
// \
++i;
cout << i;
}
So shall "++i" be part of the comment or not?
|
Yes it will.
The standard (I don't have the current one handy, this is from the April
2005 draft, but I expect the wording to be similar) says, in section 2.1
(lex.phases):
2. Each instance of a new-line character and an immediately preceding
backslash character is deleted, splicing physical source lines to form
logical source lines. If, as a result, a character sequence that
matches the syntax of a universal-character-name is produced, the
behavior is undefined. If a source file that is not empty does not end
in a new-line character, or ends in a new-line character immediately
preceded by a backslash character, the behavior is undefined.
There is nothing mentioned of any way of escaping that backslash, so
ANY backslash-newline sequence is removed, including in your comment
above.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
kanze Guest
|
Posted: Thu Oct 06, 2005 4:15 pm Post subject: Re: embedded '\' in comment |
|
|
bloodymir wrote:
| Quote: | Recently I had a problem with an array initialization. After
quite some testing I found the compiler (gcc3) was missing a
line. I tracked down the problem to a "//" comment on the line
before which ends on double backslash. It works like this:
int main() {
int i = 0;
// \
++i;
cout << i;
}
So shall "++i" be part of the comment or not?
|
It's part of the comment, at least if there is no white space
after the . See §2.1 Phases of translation, phase 2: "Each
instance of a new-line character and an immediately preceding
backslash character is deleted." This occurs before almost all
other processing: only input character code mapping, replacement
of trigraph sequences and possible generation of universal
character names for characters outside the basic character set
occurs earlier. (Note that trigraph substitution means that a
line ending with ??/ will have the same effect.)
The fact that a can "escape" an other , i.e. that '\'
represents a single backslash, only occurs in phase 5, and then
only in string and character literals.
In practice, I can't imagine the case with a trigraph actually
causing a problem; what meaningful comment could possibly end
with ??/. On the other hand, I can well imagine a comment like:
// Special treatment for
Putting a blank behind the isn't a robust solution, either,
since some systems or tools (or even some maintenance
programmers) will remove trailing whitespace from lines in text
files. The best solution I've found is that even in comments,
character constants are in single quotes, e.g.:
// Special treatment for ''
--
James Kanze GABI Software
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Maciej Sobczak Guest
|
Posted: Thu Oct 06, 2005 4:17 pm Post subject: Re: embedded '\' in comment |
|
|
bloodymir wrote:
| Quote: | int main() {
int i = 0;
// \
++i;
cout << i;
}
So shall "++i" be part of the comment or not?
|
It is part of the comment, since line are spliced (on the newline
immediately preceded by backslash) *before* comments are replaced by
single space.
--
Maciej Sobczak : http://www.msobczak.com/
Programming : http://www.msobczak.com/prog/
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
kanze Guest
|
Posted: Fri Oct 07, 2005 1:09 pm Post subject: Re: embedded '\' in comment |
|
|
Martin Bonner wrote:
| Quote: | It could be argued that line splicing should be applied AFTER
comments have been removed, but it is about two decades too
late to have that argument.
|
I think that if anyone were arguing for a change in the
standard, it would be that line splicing only applies if there
is an odd number of characters immediately preceding the new
line (and that it then only removes the last character and the
new line). Because most use of is in string or character
constants, and in string and character constants, \ is an
escape character sequence for , one rather aquires the
intuition that escapes a following , so it is taken
literally.
All in all, however, I don't think that the problem is serious
enough to be worth bothering with. The standards committee has
more important things to do.
--
James Kanze GABI Software
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
John Kewley Guest
|
Posted: Tue Oct 18, 2005 9:32 pm Post subject: Re: embedded '\' in comment |
|
|
kanze wrote:
| Quote: | ...
Putting a blank behind the isn't a robust solution, either,
since some systems or tools (or even some maintenance
programmers) will remove trailing whitespace from lines in text
files. The best solution I've found is that even in comments,
character constants are in single quotes, e.g.:
// Special treatment for ''
|
This works, although your comment [sic] about character constants
could be misinterpreted since '\' would be the appropriate char
constant. So "even in comments" is not quite right (if you are being
pedantic).
| Quote: | James Kanze GABI Software
|
(the "other") JK
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
kanze Guest
|
Posted: Thu Oct 20, 2005 3:50 am Post subject: Re: embedded '\' in comment |
|
|
John Kewley wrote:
| Quote: | kanze wrote:
...
Putting a blank behind the isn't a robust solution,
either, since some systems or tools (or even some
maintenance programmers) will remove trailing whitespace
from lines in text files. The best solution I've found is
that even in comments, character constants are in single
quotes, e.g.:
// Special treatment for ''
This works, although your comment [sic] about character
constants could be misinterpreted since '\' would be the
appropriate char constant. So "even in comments" is not quite
right (if you are being pedantic).
|
It depends on whether the character constants in question are
C++ character constants, or human character constants:-). If
I'm really talking about C++ character constants, then I will
use '\' in the comment. If I'm actually talking about specific
character values, I'll write them as a human character constant.
Actually, of course, I'm not nearly as rigorous about it as the
above would imply. Comments are for human consumption, and
human's are a lot better at parsing and understanding what I
mean than any stupid compiler. On the other hand, I find that
if you don't set off characters used as character values in some
special way, it does cause problems for human readers.
Depending on the context, I may use `x', "x", « x » (in France),
or »x« or ,,x`` (in German), but on the whole, 'x' seems to be
the easiest to type and the most appropriate when commenting C++
(or C or Java) code. Since so much of my code is C++, I've even
extended this convention to shell scripts and makefiles.
--
James Kanze GABI Software
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|