[C++0x] nullptr

Christopher Jefferson chris@bubblescope.net
Mon May 3 07:04:00 GMT 2010


On 3 May 2010, at 06:05, Ed Smith-Rowland wrote:

> Greetings,
> 
> I tried to do nullptr and I'm gratified that some of what I tried you did too ;-).
> It was a learning experience as I had never touched the front end before.
> 
> Anyway, my question is this:
> In gcc we
> #define NULL __null.
> OTOH the committee thought about and ultimately rejected
> #define NULL nullptr
> 
> Can/should we go ahead and do this since we sort of did it already?
> If we don't want to do this would it be a good idea to have a flag
> -fNULL=nullptr o somesuch?
> 

Changing NULL to nullptr does break some code. In particular, consider the following, not unreasonable, code:

void f(int*);
void f(double*);

This code will fail to compile if given NULL, regardless of if the NULL is 0 or nullptr.

To "fix" this for NULL=0, we have to add:

void f(int i) { assert(i==0); }, which is a little dodgy looking.

To fix this for NULL=nullptr, we add the nicer looking:

void f(typeof(nullptr)) { ... }

However note that without some very messy looking code, it isn't possible to fix this in such a way that it is:

a) Valid C++03 code (for people who want to stay on C++03)
b) accepts NULL=nullptr

Chris



More information about the Gcc-patches mailing list