This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: When is it legal to compare any pair of pointers?
- From: Paolo Carlini <pcarlini at suse dot de>
- To: chris jefferson <caj at cs dot york dot ac dot uk>
- Cc: gcc at gcc dot gnu dot org, Nathan Myers <ncm-nospam at cantrip dot org>
- Date: Tue, 13 Sep 2005 12:45:13 +0200
- Subject: Re: When is it legal to compare any pair of pointers?
- References: <4326A85A.8080001@cs.york.ac.uk>
chris jefferson wrote:
>I realise that according to the C++ standard it isn't legal to compare
>two pointers which are not from the same array. Is anyone aware of
>anything in g++ which would actually forbid this, and if there is any
>way of checking if will be valid?
>
In my opinion we should first close read §5.9 and 5.10. My reading is
that in general the result of pointer comparisons can be "only"
unspecified. Logically, this means that you can "trust" the result only
when both pointers point to elements of the same array.
Then, as far as *our* library (and compiler) are concerned, there is the
interesting example of basic_string::_M_disjunct: with Nathan's
substantive insight we came to the conclusion that such kind of
comparisons can be always meaningful to do (at the C++ library level) if
we use std::less<>() & co. Maybe Nathan has something to add...
Paolo.