Topic: NULL macro
Author: jeremy@jdyallop.freeserve.co.uk (Jeremy Yallop)
Date: Thu, 27 Nov 2003 17:14:41 +0000 (UTC) Raw View
[Note the crosspost and Followup-To]
James Kuyper wrote:
> It gets much worse in C++:
>
> namespace _OurNamespace
> {
> struct NullClass
"NullClass" is available to the user as a macro name.
> {
> const int value=0;
I think you need "static" here for the initialization to be valid.
Also, "value" is available to the user as a macro name.
> // other members of class
> }
Missing semicolon.
> // other members of name space
> }
> #define NULL _OurNamespace::NullClass::value
>
> The above implementation of NULL is legal in C++
Even with the above corrections, gcc (invoked as a C++ compiler)
refuses to accept your definition of NULL as a null pointer constant,
although it accepts it with a cast to int or const int. To simplify
things a bit, the following program is rejected (with the error
"invalid conversion from `int' to `void*'")
const int x = 0;
void *p = x;
int main(){}
whereas
const int x = 0;
void *p = (const int)x;
int main(){}
is accepted. Is gcc correct in this case?
Jeremy.
---
[ 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://www.jamesd.demon.co.uk/csc/faq.html ]
Author: kuyper@wizard.net (James Kuyper)
Date: Sun, 30 Nov 2003 01:36:00 +0000 (UTC) Raw View
jeremy@jdyallop.freeserve.co.uk (Jeremy Yallop) wrote in message news:<slrnbsaifn.oct.jeremy@embo.cl.cam.ac.uk>...
>
> [Note the crosspost and Followup-To]
>
> James Kuyper wrote:
> > It gets much worse in C++:
> >
> > namespace _OurNamespace
> > {
> > struct NullClass
>
> "NullClass" is available to the user as a macro name.
>
> > {
> > const int value=0;
>
> I think you need "static" here for the initialization to be valid.
> Also, "value" is available to the user as a macro name.
>
> > // other members of class
> > }
>
> Missing semicolon.
Sloppiness acknowledged. Sorry!
> > // other members of name space
> > }
> > #define NULL _OurNamespace::NullClass::value
> >
> > The above implementation of NULL is legal in C++
>
> Even with the above corrections, gcc (invoked as a C++ compiler)
> refuses to accept your definition of NULL as a null pointer constant,
> although it accepts it with a cast to int or const int. To simplify
> things a bit, the following program is rejected (with the error
> "invalid conversion from `int' to `void*'")
>
> const int x = 0;
> void *p = x;
> int main(){}
>
> whereas
>
> const int x = 0;
> void *p = (const int)x;
> int main(){}
>
> is accepted. Is gcc correct in this case?
In either C or C++, the only requirement on NULL is that it must
expand into a null pointer constant, which can (in C) or must (in C++)
be an integral constant expression with value 0. My point was that in
C++, unlike C, "An _integral constant-expression_ can involve ...
const variables or static data members of integral or enumeration
types initialized with constant expressions ..." (5.19), so the
corrected version of my example is permitted for a conforming
implementation.
---
[ 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://www.jamesd.demon.co.uk/csc/faq.html ]