title: Is &arr[size] valid?
body: <p>Let's say I have a function, called like this:</p>
  
  <pre><code>void mysort(int *arr, std::size_t size)
  {
    std::sort(&amp;arr[0], &amp;arr[size]);
  }
  
  int main()
  {
    int a[] = { 42, 314 };
    mysort(a, 2);
  }
  </code></pre>
  
  <p>My question is: does the code of <code>mysort</code> (more specifically, <code>&amp;arr[size]</code>) have defined behaviour?</p>
  
  <p>I know it would be perfectly valid if replaced by <code>arr + size</code>; pointer arithmetic allows pointing past-the-end normally. However, my question is specifically about the use of <code>&amp;</code> and <code>[]</code>.</p>
  
  <p>Per C++11 5.2.1/1, <code>arr[size]</code> is equivalent to <code>*(arr + size)</code>.</p>
  
  <p>Quoting 5.3.1/1, the rules for unary <code>*</code>:</p>
  
  <blockquote>
    <p>The unary <code>*</code> operator performs indirection: the expression to which it is applied shall be a <strong>pointer to an
    object type</strong>, or a pointer to a function type and the <strong>result is an lvalue referring to the object</strong> or function
    <strong>to which the expression points</strong>. If the type of the expression is “pointer to <code>T</code>,” the type of the result is “<code>T</code>.”
    [ <em>Note:</em> a pointer to an incomplete type (other than <em>cv</em> <code>void</code>) can be dereferenced. The lvalue thus obtained
    can be used in limited ways (to initialize a reference, for example); this lvalue must not be converted to a
    prvalue, see 4.1. <em>—end note</em> ]</p>
  </blockquote>
  
  <p>Finally, 5.3.1/3 giving the rules for <code>&amp;</code>:</p>
  
  <blockquote>
    <p>The result of the unary <code>&amp;</code> operator is a pointer to its operand. The operand shall be an lvalue ... if the type of
    the expression is <code>T</code>, the result has type “pointer to <code>T</code>” and is a prvalue that is the <strong>address of the designated object</strong> (1.7) or a pointer to the designated function.</p>
  </blockquote>
  
  <p>(Emphasis and ellipses mine).</p>
  
  <p>I can't quite make up my mind about this. I know for sure that forcing an lvalue-to-rvalue conversion on <code>arr[size]</code> would be Undefined. But no such conversion happens in the code. <code>arr + size</code> does not point to an object; but while the paragraphs above talk about objects, they never seem to explicitly call out the necessity for an object to actually exist at that location (unlike e.g. the lvalue-to-rvalue conversion in 4.1/1).</p>
  
  <p>So, the questio is: is <code>mysort</code>, the way it's called, valid or not?</p>
  
  <p>(Note that I'm quoting C++11 above, but if this is handled more explicitly in a later standard/draft, I would be perfectly happy with that).</p>