Re: !foo vs foo == nil
Re: !foo vs foo == nil
- Subject: Re: !foo vs foo == nil
- From: Scott Ribe <email@hidden>
- Date: Thu, 21 Aug 2008 16:19:26 -0600
- Thread-topic: !foo vs foo == nil
> Please show me where that is guaranteed.
Well, you've really sent me on an archeological dig. It's not in the
standard! I think there's an error of omission--yeah I know, claiming "I'm
not wrong, the standard is" is quite the claim indeed, but bear with me a
second:
6.3.2.3.5 covers integer -> pointer, and says "Except as previously
specified..." where that clearly refers to 6.3.2.3.3 converting integer
constant 0 to null pointer.
As I pointed out before, 6.3.2.3.6 covering pointer -> integer says "Except
as previously specified..." I assumed that referred to the requirement that
null pointer -> integer must yield 0, but a thorough search of the document
reveals that *nothing* was previously specified. Clearly, an error in the
standard--either of omission of whatever it was that was supposed to be
previously specified, or of editing allowing that "dangling reference".
It's also not covered in either of the first 2 Corrigenda (I couldn't find
the 3rd on the ISO site). Here's the history I have at hand:
K&R (2nd ed, 1988)
------------------
A.6.6
...
An integral constant expression with the value 0... may be converted... to a
pointer of any type. This produces a null pointer that is equal to a null
pointer of the same type, but unequal to any pointer to a function or
object.
A pointer may be converted to an integral type large enough to hold it; the
required size is implementation-dependent. The mapping function is also
implementation-dependent.
An object of integral type may be explicitly converted to a pointer. The
mapping always carries a sufficiently wide integer converted from a pointer
back to the same pointer, but is otherwise implementation-dependent.
[Doesn't explicitly state it. But implies it because constant 0 -> pointer
and variable 0 -> pointer really ought not produce different pointers, and
constant 0 -> pointer -> integer variable really ought to produce 0.]
Harbison & Steele (5th ed, 2002)
--------------------------------
6.2.3
...
When the source value is a pointer and the destination type is not _Bool,
the pointer is treated as if it were an unsigned integer of a size equal to
the size of the pointer. Then the unsigned integer is converted to the
destination type using the rules listed before. If null pointers are not
represented as the value 0, then they must be explicitly converted to 0 when
converting the null pointer to an integer.
[I suppose that's the reference I was remembering, since I did remember
reading that explicitly stated in those ~words.]
The Annotated C++ Reference Manual (1990)
-----------------------------------------
5.4
...
[Language is so similar to K&R that it's not worth repeating...]
The C++ Programming Language (Special Ed, 2000)
-----------------------------------------------
[No information at all on pointer -> integer conversions.]
--
Scott Ribe
email@hidden
http://www.killerbytes.com/
(303) 722-0567 voice
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden