-
The demo package is at https://github.com/metab0t/nanobind_demo
In C++, we can write struct S
{
std::vector<double> vec;
void push_back(double x)
{
vec.push_back(x);
}
void clear()
{
vec.clear();
}
S& operator+=(double x)
{
push_back(x);
return *this;
}
}; However, after we wrap the NB_MODULE(ext, m)
{
nb::class_<S>(m, "S")
.def(nb::init<>())
.def("push_back", &S::push_back)
.def("clear", &S::clear)
.def(nb::self += double());
} To avoid copy, we can return class XS(S):
def __iadd__(self, x):
self.push_back(x)
return self I wonder whether nanobind can optimize it automatically. |
Beta Was this translation helpful? Give feedback.
Answered by
metab0t
Mar 2, 2024
Replies: 1 comment 2 replies
-
It seems that this problem is solved by setting correct return value policy. NB_MODULE(ext, m)
{
nb::class_<S>(m, "S")
.def(nb::init<>())
.def("push_back", &S::push_back)
.def("clear", &S::clear)
.def(nb::self += double(), nb::rv_policy::none);
} I am not sure if it is the idiomatic solution. |
Beta Was this translation helpful? Give feedback.
2 replies
Answer selected by
wjakob
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It seems that this problem is solved by setting correct return value policy.
I am not sure if it is the idiomatic solution.