According to the draft of the standard N4713 (7.11/1):
A null pointer constant is an integer literal (5.13.2) with value zero or a prvalue of type
std::nullptr_t
.
and 21.2.3/2:
The macro
NULL
is an implementation-defined null pointer constant.
follow that NULL
can be defined as nullptr
. Same is mentioned on cppreference:
#define NULL 0 //since C++11 #define NULL nullptr
At the same time "Additive operators" clause says (8.5.6/7):
If the value
0
is added to or subtracted from a null pointer value, the result is a null pointer value. If two null pointer values are subtracted, the result compares equal to the value0
converted to the typestd::ptrdiff_t
.
Hence the following code should be valid:
0 + nullptr; nullptr - nullptr;
but because of the lack of +/- operators for std::nullptr_t
the code is invalid.
Is there something that I didn't take into account or NULL
macro can't be actually defined as nullptr
?
nullptr is meant as a replacement to NULL . nullptr provides a typesafe pointer value representing an empty (null) pointer. The general rule of thumb that I recommend is that you should start using nullptr whenever you would have used NULL in the past.
It is non-zero, it's true. A null pointer is zero, and so evaluates to false.
An often heard criticism of null-hypothesis significance testing is that the null is always false. The idea is that average differences between two samples will never be exactly zero (there will practically always be a tiny difference, even if it is only 0.001).
Null macro is defined in stdio. h and stddef.h.It is used to represent a null pointer in your code. its value is zero. Null pointer is same as an uninitialized pointer.. It does not point anywhere.
While nullptr
is a null pointer constant, it is not a null pointer value. The latter is a value of some pointer type, which std::nullptr_t
is not.
Reference:
A null pointer constant is an integer literal (5.13.2) with value zero or a prvalue of type
std::nullptr_t
. A null pointer constant can be converted to a pointer type; the result is the null pointer value of that type and is distinguishable from every other value of object pointer or function pointer type. Such a conversion is called a null pointer conversion. [...]
7.11/1 in N4659, emphasize mine
So NULL
can indeed be nullptr
without providing the arithmetic operators.
nullptr
is a null pointer literal, and although the result of converting nullptr
to a pointer type is the null pointer value, nullptr
itself isn't of a pointer type, but of type std::nullptr_t
. The arithmetic works if you do convert the nullptr
to a pointer type:
0 + (int*)nullptr; (int*)nullptr - (int*)nullptr;
Can the NULL macro actually be a nullptr?
Yes, because nullptr
is a null pointer literal.
Note that prior to C++11, the all of the null pointer literals in C++ happened to also be integer literals, so this bad code: char c = NULL;
used to work in practice. If NULL
is defined as nullptr
, that code no longer works.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With