5 ms·
> 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 N
by vog 15y ago
> 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?
- exDM69 15y agoIt'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.
- xilun0 15y agonot e.g. with 64-bits, va args and literal 0 (cf FrankBooth example)
- biot 15y agoYou 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.
- FrankBooth 15y agoConsider: int execl(const char *path, const char *arg, ...); called like so: execl("foo", "bar", 0); Particularly when sizeof(int) != sizeof(void *).
- doug363 15y agoIn overloaded or argument-deduced contexts, then "0" is preferentially an int, but nullptr is never an int.
- Someone 15y agoThe 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.
- pmjordan 15y agoNULL 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.
- deleted 15y ago[deleted]