For the following two classes:
class A {
};
class B {
public:
B()
: a_(std::make_shared<A>())
{}
std::shared_ptr<A> a_;
};
there is a following simple boost.python
wrapping
A& get_a(const B& b) {
return *b.a_; // intentionally, no check here; doesn't matter for the example
}
boost::python::class_<A, std::shared_ptr<A>>("A");
boost::python::class_<B>("B")
.def("get_a", &get_a, boost::python::return_internal_reference<>());
In Python there is simple members' retrieval:
import ext
b = ext.B()
a1 = b.get_a()
a2 = b.get_a()
What I expect is
id(a1) == id(a2)
which doesn't hold for the above case which means that two different PyObject
as A
wrappers are created.
Why? Doesn't return_internal_reference
policy prevents from creating multiple temporary objects?
If I return std::shared_ptr<A>
from get_a
, the programme fails to compile.
This question has similarities to this one. However, in the latter one there are temporary objects which are likely not to be kept track of. Here, on the other hand, member variables' wrappers are stored in python variables.