C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Re: NULL macro

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language, library and standards
View previous topic :: View next topic  
Author Message
Jeremy Yallop
Guest





PostPosted: Thu Nov 27, 2003 5:14 pm    Post subject: Re: NULL macro Reply with quote



[Note the crosspost and Followup-To]

James Kuyper wrote:
Quote:
It gets much worse in C++:

namespace _OurNamespace
{
struct NullClass

"NullClass" is available to the user as a macro name.

Quote:
{
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.

Quote:
// other members of class
}

Missing semicolon.

Quote:
// 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 ]


Back to top
James Kuyper
Guest





PostPosted: Sun Nov 30, 2003 1:36 am    Post subject: Re: NULL macro Reply with quote



[email]jeremy (AT) jdyallop (DOT) freeserve.co.uk[/email] (Jeremy Yallop) wrote in message news:<slrnbsaifn.oct.jeremy (AT) embo (DOT) cl.cam.ac.uk>...
Quote:

[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!

Quote:
// 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 ]


Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language, library and standards All times are GMT
Page 1 of 1

 
Jump to:  
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


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.