[ub] Justification for < not being a total order on pointers?

Nevin Liber nevin at eviloverlord.com
Wed Oct 16 18:04:26 CEST 2013


On 15 October 2013 17:39, Nevin Liber <nevin at eviloverlord.com> wrote:

> Really?  I've seen novices ask questions like "does pointer p point to an
> object inside array a?"  A natural (but wrong) way to write it is:
>
> bool isInArray = std::begin(a) <= p && p < std::end(a);
>
> Can you even write this w/o possibly invoking ub?  The only way I can
> think of is O(n), as in:
>
> bool isInArray = false;
> for (auto q = std::begin(a); q != std::end(a); ++q)
>     if (p == q) {
>         isInArray = true;
>         break;
>     }
>

Could I also please get an answer to this question?  This one isn't
theoretical, either.

It turns out that not only was this a novice question, but the novice in
question pointed to almost identical code that was believed by the author
to be C++11 compliant.  Since it isn't, how should we recommend he fix it?

I don't believe that casting it to intptr_t or uintptr_t is sufficient,
since you don't get a guarantee that the result is required to preserve the
natural order of the pointers.  Is the O(n) solution the only standards
conformant way to do it?
-- 
 Nevin ":-)" Liber  <mailto:nevin at eviloverlord.com>  (847) 691-1404
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.open-std.org/pipermail/ub/attachments/20131016/bd617ea5/attachment.html 


More information about the ub mailing list