Topic: Comparing pointer with either '\0' or char() allowed?
Author: James Kuyper <kuyper@wizard.net>
Date: 2000/07/19 Raw View
Niels Dekker wrote:
>
> Is it legal in C++ to compare a pointer with a '\0' literal?
>
> I just found the following bug in my code:
>
> void f(const char ptr[])
> {
> if( ptr == '\0' )
> {
> // From here I was assuming that ptr points to a zero character!
> // Oops :-( !!!
> }
> }
>
> All of the compilers that we currently use (at my work) accepted the
> code without a hint. Only when I tried the utterly obsolete MSVC++ 4.2,
> it gave me this weird message:
> error C2446: '==' : no conversion from 'int' to 'const char *'
> It helped my find the bug (forgetting to dereference the pointer), but
> was it right to reject the code?
No. See below.
> Later I found out that I could get the same error message with newer
> MSVC++ compilers if I replaced my '\0' literal by a char() construction:
>
> if( ptr == char() ) // error C2446 for MSVC++ 4.2, 5.3 _and_ 6.4!
>
> Still BC++ 5.5, Watcom C++ 11.0b and egcs-1.1b g++ don't complain.
> So my second question is: Is it legal to compare a pointer with a
> constructed char() object?
A '\0' and char() are both integer-valued (char is an integer type)
constant expressions with a value of 0. They are therefore null pointer
constants, and are therefore implicitly converted to null pointers of
the same type as the other operand when they occur in equality
comparisons. See section 4.4.10p1.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Biju Thomas <b.thomas@attglobal.net>
Date: 2000/07/19 Raw View
James Kuyper wrote:
>
> Niels Dekker wrote:
> >
> > Later I found out that I could get the same error message with newer
> > MSVC++ compilers if I replaced my '\0' literal by a char() construction:
> >
> > if( ptr == char() ) // error C2446 for MSVC++ 4.2, 5.3 _and_ 6.4!
> >
> > Still BC++ 5.5, Watcom C++ 11.0b and egcs-1.1b g++ don't complain.
> > So my second question is: Is it legal to compare a pointer with a
> > constructed char() object?
>
> A '\0' and char() are both integer-valued (char is an integer type)
> constant expressions with a value of 0.
Who says that "char()" is a constant expression? Any quotes?
I don't think it is. The above code [comparing pointer to "char()"]
should be ill-formed.
--
Biju Thomas
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: wmm@fastdial.net
Date: 2000/07/19 Raw View
In article <3974A48D.36642594@attglobal.net>,
b.thomas@attglobal.net wrote:
> James Kuyper wrote:
> > A '\0' and char() are both integer-valued (char is an integer type)
> > constant expressions with a value of 0.
>
> Who says that "char()" is a constant expression? Any quotes?
5.19p1 says that integral constant expressions can contain "type
conversions to integral or enumeration types." 5.2.3p2 says
that "T(), where T is a simple-type-specifier (7.1.5.2) for a
non-array complete object type" is an explicit type conversion
"whose value is determined by default initialization (8.5)."
"char()" satisfies those requirements.
--
William M. Miller, wmm@fastdial.net
Vignette Corporation (www.vignette.com)
Sent via Deja.com http://www.deja.com/
Before you buy.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: frayman <slimy@snakes.com>
Date: 2000/07/20 Raw View
On Tue, 18 Jul 2000 21:27:12 CST, Niels Dekker
<ndekker@REMOVETHISnki.nl> wrote:
>Is it legal in C++ to compare a pointer with a '\0' literal?
>
>I just found the following bug in my code:
>
> void f(const char ptr[])
> {
> if( ptr == '\0' )
> {
> // From here I was assuming that ptr points to a zero character!
> // Oops :-( !!!
> }
> }
>
>All of the compilers that we currently use (at my work) accepted the
>code without a hint. Only when I tried the utterly obsolete MSVC++ 4.2,
>it gave me this weird message:
> error C2446: '==' : no conversion from 'int' to 'const char *'
>It helped my find the bug (forgetting to dereference the pointer), but
>was it right to reject the code?
>
>Later I found out that I could get the same error message with newer
>MSVC++ compilers if I replaced my '\0' literal by a char() construction:
>
> if( ptr == char() ) // error C2446 for MSVC++ 4.2, 5.3 _and_ 6.4!
>
>Still BC++ 5.5, Watcom C++ 11.0b and egcs-1.1b g++ don't complain.
>So my second question is: Is it legal to compare a pointer with a
>constructed char() object?
>
>
>Thanks in advance
>(and another thank you for your replies on my "magic" floating point
>postings!)
>
>Niels Dekker
>ndekker "at" nki "dot" nl
>
>---
>[ comp.std.c++ is moderated. To submit articles, try just posting with ]
>[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
>[ --- Please see the FAQ before posting. --- ]
>[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
actually it' not a '0' (0x30) character, but byte 0 or NULL. I asume
that the compiler checks it as a doing a comparasion between ptr and
NULL, same as if (ptr == NULL) ... to test this I would say set ptr to
NULL and see if you take the if branch. In C I would say it's
probably not illegal, however in C++ there is a lot more type checking
and I would have thougth that it would have been illegal..
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: fjh@cs.mu.OZ.AU (Fergus Henderson)
Date: 2000/07/20 Raw View
Biju Thomas <b.thomas@attglobal.net> writes:
>James Kuyper wrote:
>>
>> Niels Dekker wrote:
>> >
>> > Later I found out that I could get the same error message with newer
>> > MSVC++ compilers if I replaced my '\0' literal by a char() construction:
>> >
>> > if( ptr == char() ) // error C2446 for MSVC++ 4.2, 5.3 _and_ 6.4!
>> >
>> > Still BC++ 5.5, Watcom C++ 11.0b and egcs-1.1b g++ don't complain.
>> > So my second question is: Is it legal to compare a pointer with a
>> > constructed char() object?
>>
>> A '\0' and char() are both integer-valued (char is an integer type)
>> constant expressions with a value of 0.
>
>Who says that "char()" is a constant expression? Any quotes?
>
>I don't think it is.
Well, I would guess that this is an issue that the committee did not
consider; the existing wording is certainly not very clear about
that case. It would probably be a good idea to submit a defect
report about it.
But, if you really want a quote: the syntax `char()' is defined in
section 5.2.3, which is named "Explicit type conversion", and so that
could be taken to imply that that syntax denotes a type conversion;
and 5.19 [expr.const] paragraph 1 says "Only type conversions to
integral types may be used", which could be taken to imply that using
`char()' is OK in constant expressions, since it is a type conversion
to an integral type.
This chain of reasoning is somewhat strained, so I think the wording
ought to be clarified to make it clear whether or not expressions
like `char()' are integral constants.
--
Fergus Henderson <fjh@cs.mu.oz.au> | "I have always known that the pursuit
WWW: <http://www.cs.mu.oz.au/~fjh> | of excellence is a lethal habit"
PGP: finger fjh@128.250.37.3 | -- the last words of T. S. Garp.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Biju Thomas <b.thomas@attglobal.net>
Date: 2000/07/20 Raw View
wmm@fastdial.net wrote:
>
> In article <3974A48D.36642594@attglobal.net>,
> b.thomas@attglobal.net wrote:
> > James Kuyper wrote:
> > > A '\0' and char() are both integer-valued (char is an integer type)
> > > constant expressions with a value of 0.
> >
> > Who says that "char()" is a constant expression? Any quotes?
>
> 5.19p1 says that integral constant expressions can contain "type
> conversions to integral or enumeration types." 5.2.3p2 says
> that "T(), where T is a simple-type-specifier (7.1.5.2) for a
> non-array complete object type" is an explicit type conversion
> "whose value is determined by default initialization (8.5)."
>
> "char()" satisfies those requirements.
5.19p1 also says that "integral constant expression can involve only
literals, enumerators, const variables or static data members of
integral or enumeration types initialized with constant expressions,
non-type template parameters of integral or enumeration types, and
sizeof expressions". The expression "char()" doesn't contain any of
these.
I think that any type conversion to integral or enumeration types (as
you quote) should be applied to such integral constant expression to get
another integral constant expression. Otherwise, we can apply this type
conversion rule to any arbitrary expression and claim that the result is
an integral constant expression, which clearly is not the case.
So, is there any requirement that a default initialized integral type
(such as "char()") can be considered constant integral expression?
--
Biju Thomas
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Ron Natalie <ron@sensor.com>
Date: 2000/07/20 Raw View
Biju Thomas wrote:
>
> So, is there any requirement that a default initialized integral type
> (such as "char()") can be considered constant integral expression?
>
Are you telling me that
char(0)
should be legal as the text currently reads but
char()
should not be?
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: wmm@fastdial.net
Date: 2000/07/20 Raw View
In article <3975C9B0.4A424773@attglobal.net>,
b.thomas@attglobal.net wrote:
> wmm@fastdial.net wrote:
> >
> > In article <3974A48D.36642594@attglobal.net>,
> > b.thomas@attglobal.net wrote:
> > > James Kuyper wrote:
> > > > A '\0' and char() are both integer-valued (char is an integer
type)
> > > > constant expressions with a value of 0.
> > >
> > > Who says that "char()" is a constant expression? Any quotes?
> >
> > 5.19p1 says that integral constant expressions can contain "type
> > conversions to integral or enumeration types." 5.2.3p2 says
> > that "T(), where T is a simple-type-specifier (7.1.5.2) for a
> > non-array complete object type" is an explicit type conversion
> > "whose value is determined by default initialization (8.5)."
> >
> > "char()" satisfies those requirements.
>
> 5.19p1 also says that "integral constant expression can involve only
> literals, enumerators, const variables or static data members of
> integral or enumeration types initialized with constant expressions,
> non-type template parameters of integral or enumeration types, and
> sizeof expressions". The expression "char()" doesn't contain any of
> these.
It doesn't contain anything that's _not_ in the list, either,
except for a type conversion, and type conversions are
obviously allowed by the sentence I quoted. The way I think
"can involve only..." should be interpreted is that anything
not in the list would disqualify the expression; I don't see
any reason to take it as requiring that _at least_ one of the
items from the list must appear.
> I think that any type conversion to integral or enumeration types (as
> you quote) should be applied to such integral constant expression to
get
> another integral constant expression. Otherwise, we can apply this
type
> conversion rule to any arbitrary expression and claim that the result
is
> an integral constant expression, which clearly is not the case.
No, the "can involve only..." rules out "any arbitrary
expression", whether converted to an integral or enumeration
type or not. (And, BTW, type conversions can be applied to
more than just integral constant expressions: the literal
"1.0" is not an integral constant expression, but "int(1.0)"
is.)
> So, is there any requirement that a default initialized integral type
> (such as "char()") can be considered constant integral expression?
I can't think of any. At the same time, I don't see any
reason to rule it out, either -- the expression "char()" is
as computable at compile time as the expression "char(0)".
--
William M. Miller, wmm@fastdial.net
Vignette Corporation (www.vignette.com)
Sent via Deja.com http://www.deja.com/
Before you buy.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Biju Thomas <b.thomas@attglobal.net>
Date: 2000/07/20 Raw View
Ron Natalie wrote:
>
> Are you telling me that
> char(0)
> should be legal as the text currently reads but
> char()
> should not be?
After reading 5.2.3/1 and 5.2.3/2, I think they are different things
when used in the context of the OP's code.
"char(0)" is equivalent to "(char)0" and hence a constant integral
expression. "char()" creates a default-initialized rvalue of type char
and the standard doesn't say this is a constant integral expression.
--
Biju Thomas
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: wmm@fastdial.net
Date: 2000/07/20 Raw View
In article <8l3slj$o7n$1@mulga.cs.mu.OZ.AU>,
fjh@cs.mu.OZ.AU (Fergus Henderson) wrote:
> Biju Thomas <b.thomas@attglobal.net> writes:
> >Who says that "char()" is a constant expression? Any quotes?
> >
> >I don't think it is.
>
> Well, I would guess that this is an issue that the committee did not
> consider; the existing wording is certainly not very clear about
> that case. It would probably be a good idea to submit a defect
> report about it.
...
> This chain of reasoning is somewhat strained, so I think the wording
> ought to be clarified to make it clear whether or not expressions
> like `char()' are integral constants.
I'll check with the Committee to see if there's any
controversy about it or interest in treating it as an issue.
--
William M. Miller, wmm@fastdial.net
Vignette Corporation (www.vignette.com)
Sent via Deja.com http://www.deja.com/
Before you buy.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: "David J. Littleboy" <davidjl@gol.com>
Date: 2000/07/21 Raw View
"Niels Dekker" <ndekker@REMOVETHISnki.nl> wrote:
> Is it legal in C++ to compare a pointer with a '\0' literal?
> void f(const char ptr[])
> {
> if( ptr == '\0' )
Well, yes it's legal. But it's not what you want. Since what you wanted to
ask is "does ptr point to the null character" (i.e. is ptr[0] == '\0') then
you need to dereference ptr, i.e.:
if( (*ptr) == '\0' )
Of course, this is somewhat perverse (since ptr is declared as an array) and
if( ptr[0] == '\0' )
would be much more friendly to folks reading your code later (IMO). Of
course, naming an array "ptr" is also perverse. You should decide which
abstraction you are using. Even better, use std::string.)
> So my second question is: Is it legal to compare a pointer with a
> constructed char() object?
You still haven't dereferenced your pointer, so even if it were, it's not
what you want.
David J. Littleboy
Tokyo, Japan
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Niels Dekker <ndekker@REMOVETHISnki.nl>
Date: 2000/07/18 Raw View
Is it legal in C++ to compare a pointer with a '\0' literal?
I just found the following bug in my code:
void f(const char ptr[])
{
if( ptr == '\0' )
{
// From here I was assuming that ptr points to a zero character!
// Oops :-( !!!
}
}
All of the compilers that we currently use (at my work) accepted the
code without a hint. Only when I tried the utterly obsolete MSVC++ 4.2,
it gave me this weird message:
error C2446: '==' : no conversion from 'int' to 'const char *'
It helped my find the bug (forgetting to dereference the pointer), but
was it right to reject the code?
Later I found out that I could get the same error message with newer
MSVC++ compilers if I replaced my '\0' literal by a char() construction:
if( ptr == char() ) // error C2446 for MSVC++ 4.2, 5.3 _and_ 6.4!
Still BC++ 5.5, Watcom C++ 11.0b and egcs-1.1b g++ don't complain.
So my second question is: Is it legal to compare a pointer with a
constructed char() object?
Thanks in advance
(and another thank you for your replies on my "magic" floating point
postings!)
Niels Dekker
ndekker "at" nki "dot" nl
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]