title: C++: casting contents of a std::vector as a reference to an array
body: <p>I have an existing C++ API function that looks like this:</p>
  
  <pre><code>void do_something(const Foo (&amp;input_array)[N]);
  </code></pre>
  
  <p>I think that this takes an array of N contiguously allocated Foo structures, by reference. For example, I can pass in an array:</p>
  
  <pre><code>Foo my_array[N] = { ... }
  do_something(my_array);
  </code></pre>
  
  <p>In my real code however I have a <code>std::vector&lt;Foo&gt;</code>, the contents of which I understand can be treated like an array using the <code>.data()</code> member function, however the following results in a compiler (clang) error:</p>
  
  <pre><code>// Say N is 2:
  std::vector&lt;Foo&gt; my_vector(N);
  // ...initialise vector values...
  do_something(*my_vector.data());
  </code></pre>
  
  <p>It complains that there's no viable conversion from <code>const Foo</code> to <code>const Foo [2]</code>.</p>
  
  <p>I understand that C++ considers array sizes to be considered part of the type. In this case, what would be the correct type cast? I got as far as this:</p>
  
  <pre><code>  auto my_vector_as_array = static_cast&lt;const Foo &amp; [N]&gt;(*my_vector.data());
  </code></pre>
  
  <p>However the compiler is not happy at all about the use of <code>[N]</code> or even <code>[]</code> as part of the static cast's type parameter, complaining that:</p>
  
  <pre><code>error: 'type name' declared as array of references of type 'const Foo &amp;'
  </code></pre>
  
  <p>Is there a way to correctly coerce the contents of a <code>std::vector</code> into this function?</p>
  
  <p>Here's the full code:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;vector&gt;
  #define N 4
  
  struct Foo { int a; int b; float c; };
  
  void do_something(const Foo (&amp;input_array)[N]) {
    for (size_t i = 0; i &lt; N; ++i) {
      std::cout &lt;&lt; i &lt;&lt; ": " &lt;&lt; input_array[i].a &lt;&lt; ", " &lt;&lt; input_array[i].b &lt;&lt; ", " &lt;&lt; input_array[i].c &lt;&lt; std::endl;
    }
  }
  
  int main(int argc, char * argv[]) {
    // pass a C-style array by reference:
    Foo my_array[N] = { {1, 10, 100.0}, {2, 20, 200.0}, {3, 30, 300.0}, {4, 40, 400.0} };
    do_something(my_array);
  
    // try to do the same with the contents of a vector:
    std::vector&lt;Foo&gt; my_vector(N);
    my_vector[0] = {1, 10, 100.0};
    my_vector[1] = {2, 20, 200.0};
    my_vector[2] = {3, 30, 300.0};
    my_vector[3] = {4, 40, 400.0};
  
    // this fails to compile:
    do_something(*my_vector.data());
  
    // try casting via a temporary variable - also fails to compile:
    auto my_vector_as_array = static_cast&lt;const Foo &amp; []&gt;(*my_vector.data());
    do_something(my_vector_as_array);
  }
  </code></pre>
  
  <p>EDIT: it has been suggested that this is a duplicate of <a href="https://stackoverflow.com/questions/6701816">this question</a> however the solutions presented there do not work in this case. I think this may be because the function defines the array reference with an explicit size, and the compiler is not happy accepting a mere pointer (because the size is lost from the type).</p>