title: pybind11 array from 2d vector without copy
body: <p>How can I create a 2d numpy array from a vector of vectors in pybind11?</p>
  
  <p>I'm looking for a way to do this fast &amp; without copy (so move?), and I'd like to do it in a templated fashion.</p>
  
  <p>So far I have:</p>
  
  <pre><code>template&lt; typename T &gt;
  py::array
  array2d_from_vector(std::vector&lt;std::vector&lt;T&gt;&gt; &amp; m) {
      if (m.empty()) return py::array_t&lt;T&gt;();
      std::vector&lt;std::vector&lt;T&gt;&gt;* ptr = new std::vector&lt;std::vector&lt;T&gt;&gt;(std::move(m));
      auto capsule = py::capsule(ptr, [](void* p) {
              delete reinterpret_cast&lt;std::vector&lt;std::vector&lt;T&gt;&gt;*&gt;(p);
      });
      return py::array_t&lt;T&gt;(
              {ptr-&gt;size(), ptr-&gt;at(0).size()},           // shape of array
              {ptr-&gt;at(0).size()*sizeof(T), sizeof(T)},   // c-style contiguous strides
              capsule);
  }
  </code></pre>
  
  <p>which returns a nonsense 2d array to python with values like:</p>
  
  <pre><code>[[ 4.0852747e+30  3.0677226e-41  4.0853762e+30  3.0677226e-41]...
  </code></pre>
  
  <p>So what am I missing! I'm guessing it has to do with the way vectors hold their data, but my values should be between 0 and 1.</p>