Can the NULL macro actually be a nullptr?
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 value 0
converted to the type std::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
?
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.
For addition, either both operands shall have arithmetic or unscoped enumeration type, or one operand shall be a pointer to a completely-defined object type and the other shall have integral or unscoped enumeration type.
For subtraction, one of the following shall hold:
(2.1) both operands have arithmetic or unscoped enumeration type; or
(2.2) both operands are pointers to cv-qualified or cv-unqualified versions of the same completely-defined object type; or
(2.3) the left operand is a pointer to a completely-defined object type and the right operand has integral or unscoped enumeration type.
std::nullptr_t
is none of those, hence std::nullptr
cannot participate in additive operations.
Note that not even all pointer values can participate. For example, function pointer values and void pointer values cannot, even though either can be a null pointer value.
链接地址: http://www.djcxy.com/p/73016.html上一篇: 我应该如何编写符合ISO C ++标准的自定义新的和删除操作符?
下一篇: NULL宏实际上可以是nullptr吗?