<div dir="ltr"><div class="gmail_extra">On 15 October 2013 17:39, Nevin Liber <span dir="ltr">&lt;<a href="mailto:nevin@eviloverlord.com" target="_blank">nevin@eviloverlord.com</a>&gt;</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&#39;ve seen novices ask questions like &quot;does pointer p point to an object inside array a?&quot;  A natural (but wrong) way to write it is:<br>


<br></div><div>bool isInArray = std::begin(a) &lt;= p &amp;&amp; p &lt; 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&#39;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&#39;t, how should we recommend he fix it?<br>

<br></div><div class="gmail_extra">I don&#39;t believe that casting it to intptr_t or uintptr_t is sufficient, since you don&#39;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 &quot;:-)&quot; Liber  &lt;mailto:<a href="mailto:nevin@eviloverlord.com" target="_blank">nevin@eviloverlord.com</a>&gt;  (847) 691-1404
</div></div>