Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

> explicit strong nullptr constant; no more NULL macro nonsense

What is the point of this? C++ defines the null pointer to be always 0. So I never needed the NULL macro in C++ anyway, as I'm allowed to simply type 0 instead.

In how far is that new nullptr constant preferable to writing simply 0?



The standard example is:

    void f(int x);
    void f(char *x);
    ...
    f(0);  // calls void f(int)
That may seem contrived, but you may not know of the f(int) overload, especially in combination with templates.

NULL, if #define'd as (void *)0, prevents that error.


NULL is actually #defined to 0 in C++ as void* is not implicitly convertible to any pointer type as it is in C. So unlike in C there has been no safety in using NULL. Until now.


Consider:

  int execl(const char *path, const char *arg, ...);
called like so:

  execl("foo", "bar", 0);
Particularly when sizeof(int) != sizeof(void *).


You may know that a null pointer is always 0, but you don't know that 0 is always a null pointer -- it may be the result of subtracting an integer from itself. That's the difference.


In overloaded or argument-deduced contexts, then "0" is preferentially an int, but nullptr is never an int.


It's all about compile time warnings and static type information. The resulting code should be the same whether you're using 0, NULL or nullptr.


not e.g. with 64-bits, va args and literal 0 (cf FrankBooth example)




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: