title: C++ function returning reference to array
body: <p>Is there any other way to receive a reference to an array from function returning except using a pointer?</p>
  
  <p>Here is my code.</p>
  
  <pre><code>int ia[] = {1, 2, 3};
  decltype(ia) &amp;foo() {   // or, int (&amp;foo())[3]
      return ia;
  }
  
  int main() {
      int *ip1 = foo();   // ok, and visit array by ip1[0] or *(ip1 + 0)
      auto ip2 = foo();   // ok, the type of ip2 is int *
      int ar[] = foo();   // error
      int ar[3] = foo();  // error
      return 0;
  }
  </code></pre>
  
  <p>And a class version.</p>
  
  <pre><code>class A {
  public:
      A() : ia{1, 2, 3} {}
      int (&amp;foo())[3]{ return ia; }
  private:
      int ia[3];
  };
  
  int main() {
      A a;
      auto i1 = a.foo();    // ok, type of i1 is int *, and visit array by i1[0]
      int i2[3] = a.foo();  // error
      return 0;
  }
  </code></pre>
  
  <p>Note: <code>const</code> qualifier is omitted in code.</p>
  
  <p><strike>I know the name of the array is a pointer to the first element in that array, so using a pointer to receive is totally viable.</strike></p>
  
  <p>Sorry, I made a mistake. From <a href="http://en.cppreference.com/w/cpp/language/array#Array_to_pointer_decay" rel="noreferrer">Array to pointer decay</a></p>
  
  <blockquote>
    <p>There is an implicit conversion from lvalues and rvalues of array type to rvalues of pointer type: it constructs a pointer to the first element of an array.</p>
  </blockquote>
  
  <p>Please ignore that XD</p>
  
  <p>I'm just curious about the question I asked at the beginning :)</p>