returning numpy arrays via pybind11 : <p>I have a C++ function computing a large tensor which I would like to return to Python as a NumPy array via <a href="https://github.com/pybind/pybind11" rel="noreferrer">pybind11</a>. </p>

<p>From the documentation of pybind11, it seems like using <a href="http://en.cppreference.com/w/cpp/memory/unique_ptr" rel="noreferrer">STL unique_ptr </a> is desirable. 
In the following example, the commented out version works, whereas the given one compiles but fails at runtime ("Unable to convert function return value to a Python type!"). </p>

<p>Why is the smartpointer version failing? What is the canonical way to create and return a NumPy array?</p>

<p>PS: Due to program structure and size of the array, it is desirable to not copy memory but create the array from a given pointer. Memory ownership should be taken by Python.</p>

<pre class="lang-cpp prettyprint-override"><code>typedef typename py::array_t&lt;double, py::array::c_style | py::array::forcecast&gt; py_cdarray_t;

// py_cd_array_t _test()
std::unique_ptr&lt;py_cdarray_t&gt; _test()
{
    double * memory = new double[3]; memory[0] = 11; memory[1] = 12; memory[2] = 13;
    py::buffer_info bufinfo (
        memory,                                   // pointer to memory buffer
        sizeof(double),                           // size of underlying scalar type
        py::format_descriptor&lt;double&gt;::format(),  // python struct-style format descriptor
        1,                                        // number of dimensions
        { 3 },                                    // buffer dimensions
        { sizeof(double) }                        // strides (in bytes) for each index
    );

    //return py_cdarray_t(bufinfo);
    return std::unique_ptr&lt;py_cdarray_t&gt;( new py_cdarray_t(bufinfo) );
}
</code></pre>
