Skip to content

Wrapping a unique_ptr reference #3752

Answered by jiwaszki
MarAlder asked this question in Q&A
Discussion options

You must be logged in to vote

@MarAlder I extended your code a little bit.

Most important thing is to notice how vector is moved -- please look it up why vectors of unique pointers are movable but not copyable. Moreover you need to create new Child before moving it inside the vector itself. "Temporary" unique pointer cannot be passed inside push_back.

Second important change are return policies added to Parent's bindings, these are critical since omitting them is resulting in memory deallocation errors (refer to pybind docs here).

Here is the sample:

#include <vector>

#include <pybind11/pybind11.h>
#include <pybind11/stl.h>

namespace py = pybind11;

class Child{

public:
    Child(int num) : _num(num) {}

    std::s…

Replies: 2 comments 1 reply

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
1 reply
@MarAlder
Comment options

Answer selected by MarAlder
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants