title: Why aren't variable-length arrays part of the C++ standard?
body: <p>I haven't used C very much in the last few years. When I read <a href="https://stackoverflow.com/questions/1886504/coding-problem-using-a-2-d-array-of-structs-inside-another-struct-in-c">this question</a> today I came across some C syntax which I wasn't familiar with.</p>
  
  <p>Apparently in <a href="https://en.wikipedia.org/wiki/C99" rel="noreferrer">C99</a> the following syntax is valid:</p>
  
  <pre><code>void foo(int n) {
      int values[n]; //Declare a variable length array
  }
  </code></pre>
  
  <p>This seems like a pretty useful feature. Was there ever a discussion about adding it to the C++ standard, and if so, why it was omitted?</p>
  
  <p>Some potential reasons:</p>
  
  <ul>
  <li>Hairy for compiler vendors to implement</li>
  <li>Incompatible with some other part of the standard</li>
  <li>Functionality can be emulated with other C++ constructs</li>
  </ul>
  
  <p>The C++ standard states that array size must be a constant expression (8.3.4.1).</p>
  
  <p>Yes, of course I realize that in the toy example one could use <code>std::vector&lt;int&gt; values(m);</code>, but this allocates memory from the heap and not the stack. And if I want a multidimensional array like:</p>
  
  <pre><code>void foo(int x, int y, int z) {
      int values[x][y][z]; // Declare a variable length array
  }
  </code></pre>
  
  <p>the <code>vector</code> version becomes pretty clumsy:</p>
  
  <pre><code>void foo(int x, int y, int z) {
      vector&lt; vector&lt; vector&lt;int&gt; &gt; &gt; values( /* Really painful expression here. */);
  }
  </code></pre>
  
  <p>The slices, rows and columns will also potentially be spread all over memory.</p>
  
  <p>Looking at the discussion at <code>comp.std.c++</code> it's clear that this question is pretty controversial with some very heavyweight names on both sides of the argument. It's certainly not obvious that a <code>std::vector</code> is always a better solution.</p>