<div dir="ltr"><div class="gmail_extra">On 15 October 2013 17:39, Nevin Liber <span dir="ltr"><<a href="mailto:nevin@eviloverlord.com" target="_blank">nevin@eviloverlord.com</a>></span> wrote:<br><div class="gmail_quote">
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div>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:<br>
<br></div><div>bool isInArray = std::begin(a) <= p && p < std::end(a);</div><div><br></div><div>Can you even write this w/o possibly invoking ub? The only way I can think of is O(n), as in:<br><br></div><div>
bool isInArray = false;<br></div><div>for (auto q = std::begin(a); q != std::end(a); ++q)<br></div><div> if (p == q) {<br></div><div> isInArray = true;<br></div><div> break;<br> }</div></blockquote></div>
<br></div><div class="gmail_extra">Could I also please get an answer to this question? This one isn't theoretical, either.<br><br></div><div class="gmail_extra">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?<br>
<br></div><div class="gmail_extra">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?<br>
-- <br> Nevin ":-)" Liber <mailto:<a href="mailto:nevin@eviloverlord.com" target="_blank">nevin@eviloverlord.com</a>> (847) 691-1404
</div></div>