title: Pass heap-based N-ary array reference to function
body: <p>I am trying to pass an N-ary array to a function in C++.</p>
  
  <p>Keeping it simple, for a 1D array, here's a :</p>
  
  <pre><code>#include &lt;iostream&gt;
  
  template &lt;size_t N&gt; int sign(int(&amp;arr)[N], int i)
  {
      return (arr[i] &gt;= 0) ? +1 : -1 ;
  }
  
  int main(int argn, char** argv)
  {
      const int SIZE = 2;
  
      int (*arr1d) = new int[SIZE];
      arr1d[0] = 12;
      arr1d[1] = -1;
  
      std::cout &lt;&lt; "sign 0 : " &lt;&lt; sign(arr1d, 0) &lt;&lt; std::endl ;     
      std::cout &lt;&lt; "sign 1 : " &lt;&lt; sign(arr1d, 1) &lt;&lt; std::endl ;
  }
  </code></pre>
  
  <p>But this won't compile:</p>
  
  <pre><code>Sign.cpp:17:50: error: no matching function for call to ‘sign(int*&amp;, int)’
       std::cout &lt;&lt; "sign 1 : " &lt;&lt; sign(arr1d, 1) &lt;&lt; std::endl ;
                                                ^
  Sign.cpp:3:29: note: candidate: template&lt;long unsigned int N&gt; int sign(int (&amp;)[N], int)
   template &lt;size_t N&gt; int sign(int(&amp;arr)[N], int i)
                           ^~~~
  Sign.cpp:3:29: note:   template argument deduction/substitution failed:
  Sign.cpp:17:50: note:   mismatched types ‘int [N]’ and ‘int*’
       std::cout &lt;&lt; "sign 1 : " &lt;&lt; sign(arr1d, 1) &lt;&lt; std::endl ;
  </code></pre>
  
  <p>I don't mind <em>not</em> using templates (which seems to be the problem here).</p>
  
  <p>Any suggestion?</p>
  
  <hr>
  
  <p>Same error for the 2D case:</p>
  
  <pre><code>#include &lt;iostream&gt;
  
  template &lt;size_t M, size_t N&gt; int sign(int(&amp;arr)[M][N], int i, int j)
  {
      return (arr[i][j] &gt;= 0) ? +1 : -1 ;
  }
  
  int main(int argn, char** argv)
  {
      const int SIZE = 2;
  
      int (*arr2d)[SIZE] = new int[SIZE][SIZE];                           
      arr2d[0][0] = 12;
      arr2d[0][1] = -1;
      arr2d[1][0] = 32;
      arr2d[1][1] = -4;
  
      std::cout &lt;&lt; "sign 0 1: " &lt;&lt; sign(arr2d, 0, 1) &lt;&lt; std::endl ;
  }
  </code></pre>
  
  <hr>