The CPPReference page on reinterpret_cast
mentions possible multiple distinct values of std::nullptr_t
. The page for that doesn't rule that out, but also doesn't explain what such a value might be. I also couldn't find anything from a cursory web search. Unfortunately I don't have a copy of the C++ standard :(
What other values beside nullptr
can exist of type nullptr_t
?
Are any of them standard?
How can they be created?
Do they behave any differently from nullptr
?
The standard does not state how many different values of type std::nullptr_t
there are. If there were such a specification, it would be in [basic.fundamental], but the third paragraph of that section merely states
The types denoted by cv
std::nullptr_t
are distinct types. A prvalue of typestd::nullptr_t
is a null pointer constant ([conv.ptr]). Such values participate in the pointer and the pointer-to-member conversions ([conv.ptr], [conv.mem]).sizeof(std::nullptr_t)
shall be equal tosizeof(void*)
.
Two values of type std::nullptr_t
can be compared; the result is specified by [expr.eq]/5:
Two operands of type
std::nullptr_t
or one operand of typestd::nullptr_t
and the other a null pointer constant compare equal.
This implies that whether or not two std::nullptr_t
objects have the same value is unobservable. They will compare equal with each other regardless of value. (Edit: In fact, performing an lvalue-to-rvalue conversion on a glvalue of type cv std::nullptr_t
ignores the value stored in the object and simply gives you a null pointer constant.) It may be that all bits of std::nullptr_t
are padding, implying that all bit patterns represent the same value. Or, it may be that all bits are value bits, and there could be 264 different values of std::nullptr_t
but they can't be distinguished using comparison operators. (And all of them convert to a null pointer.) It may be that there are one or more value bits but all value representations except one are invalid.
Note that if there are any padding bits in std::nullptr_t
, or if there are any invalid value representations, there is possibility of UB when a std::nullptr_t
value is passed to or returned by the std::bit_cast
function.