Sujet : Re: question about nullptr
De : ben (at) *nospam* bsb.me.uk (Ben Bacarisse)
Groupes : comp.lang.cDate : 09. Jul 2024, 11:14:41
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <874j8yswha.fsf@bsb.me.uk>
References : 1 2 3 4 5 6 7
User-Agent : Gnus/5.13 (Gnus v5.13)
"Chris M. Thomasson" <
chris.m.thomasson.1@gmail.com> writes:
p = nullptr;
(p having been declared void *)
(0 == p == nullptr == NULL == 0) == true ?
>
Am I missing something here? If so, here is a preemptive: Shit!
You are missing that 0 == p == nullptr == NULL == 0 does not mean what
you want! It means
(((0 == p) == nullptr) == NULL) == 0
and that is a constraint violation.
Why? Well 0 == p has value 1 and is of type int and equality
comparisons between int and nullptr_t values (of which there is only
one) are not permitted. Catching this sort of thing is one of the
benefits of nullptr and its associated type nullptr_t. It means that
while
#define nullptr ((void *)0)
can help to get C23 code to compile with a pre-C23 compiler, it might
result in some C23 constraint violations going undetected.
Anyway, that aside, I know what you meant. To clarify, all of the
following have the value 1:
0 == p
p == nullptr
nullptr == NULL
NULL == 0
0 == nullptr
!p
Note that 0 == nullptr /is/ allowed even though 0 has type int. That is
because 0 is also a null pointer constant, and the rules for == and !=
specifically allow it.
-- Ben.