Search code examples
c++nulllanguage-lawyernullptr

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?


Solution

  • 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.