I am attempting to return a const reference to a vector of unique ptrs from a C++ library to a python interface. I am trying code similar to the below, but I am getting compilation errors when trying to call py::bind_vector<std::vector<std::unique_ptr<A>>>
#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
#include <pybind11/stl_bind.h>
class A
{
public:
A( int x ){ i = x; }
A(const A&) = delete;
A& operator=(const A&) = delete;
int getI() { return i; }
private:
int i;
};
class Test
{
public:
Test(){
for(int i = 0; i < 10; ++i) {
avec_.emplace_back( std::make_unique<A>(i));
}
}
Test( const Test& ) = delete;
Test& operator=(const Test& ) = delete;
const std::vector<std::unique_ptr<A>>& getVec() const { return avec_; }
private:
std::vector<std::unique_ptr<A>> avec_;
};
PYBIND11_MODULE(UniqContainer, m) {
py::bind_vector<std::vector<std::unique_ptr<A>>>(m, "VecA", py::module_local() );
py::class_<A>(m, "A")
.def( py::init<int>() )
.def( "getI", &A::getI );
py::class_<Test>(m, "Test" )
.def( py::init<>() )
.def( "getVec", &Test::getVec, py::return_value_policy::reference_internal );
}
My question is - is it possible to return a const reference to std::vector<std::unique_ptr<A>>
in python bindings?
EDIT: Added:
- copy ctor and assignment operator delete
py::return_value_policy::reference_internal