This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
When is it legal to compare any pair of pointers?
- From: chris jefferson <caj at cs dot york dot ac dot uk>
- To: gcc at gcc dot gnu dot org
- Date: Tue, 13 Sep 2005 11:22:18 +0100
- Subject: When is it legal to compare any pair of pointers?
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?
I want to be able to perform two main operations. Firstly to compare any
pair of pointers with ==, and also to write code like:
template<typename T>
bool
in_range(T* begin, T* end, T* value)
{ return (begin <= value) != (end <= value); }
Where value may be a pointer not from the same array as begin and end.
Apologises for sending this question to the main gcc list, but I want to
submit such code to the debugging part of libstdc++-v3, and wanted to
check if any optimisations may make use the fact comparing pointers from
different arrays is undefined.
Thank you,
Chris