[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: comparing function pointer with int does not produce error



On Tue, Oct 23, 2001 at 11:51:46PM +0200, Martin v. Loewis wrote:
> > On Tue, Oct 23, 2001 at 08:21:26PM +0200, Martin v. Loewis wrote:
> > > > 	The following code does not produce an error when one is expected.
> > > > void stat ();
> > > > 
> > > > static const int success = 0;
> > > > 
> > > > void monk ()
> > > > {
> > > >   if (stat == success);
> > > > };
> > > 
> > > Thanks for your report. This is not a bug in the compiler: both the
> > > function pointer and the integer convert to bool (4.12). So this is a
> > > boolean comparison, and thus well-formed C++.
> > 
> > Really?
> 
> You are right, I was confused (I thought of the operator signatures in
> 13.6/12, ignoring that those aren't used if none of the arguments was
> of class type).
> 
> I'll take another attempt at declaring gcc behaviour to be correct.
> According to 4.10/1, "success" is a null pointer constant:
> 
> # A null pointer constant is an integral constant expression (5.19)
> # rvalue of integer type that evaluates to zero. A null pointer
> # constant can be converted to a pointer type; the result is the null
> # pointer value of that type and is distinguishable from every other
> # value of pointer to object or pointer to function type.
> 
> 5.19/1 defines "integral constant expressions":
> 
> # An integral constant-expression can involve only literals (2.13),
> # enumerators, const variables or static data members of integral or
> # enumeration types initialized with constant expressions (8.5),
> # non-type template parameters of integral or enumeration types, and
> # sizeof expressions.
> 
> Since "success" is an integral constant expression of integer type, it
> is a null pointer constant, and can be converted to void(*)().  So
> this is a function pointer comparison, and thus well-formed C++ :-)

I don't think you're arguing the right side.  Look at my message - I
don't know what the poster did, but _MY_ installed g++ v3.0 declares it
invalid.  I suppose that the argument you present above COULD apply,
but considering 'static const int s = 0;' a NULL pointer constant seems
very wrong to me somehow :)

-- 
Daniel Jacobowitz                           Carnegie Mellon University
MontaVista Software                         Debian GNU/Linux Developer



Reply to: