-
|
I'm trying to port my library from pybind11 to nanobind. Not sure how to deal with a custom constructor I have that takes parameters: nb::class_<fastgpx::Bounds>(m, "Bounds")
.def(nb::init<>())
.def(nb::init<const fastgpx::LatLong&, const fastgpx::LatLong&>(), nb::arg("min"),
nb::arg("max"))
// Allow tuples instead of explicit LatLong objects.
.def(nb::init([](std::tuple<double, double> min_tuple, std::tuple<double, double> max_tuple) {
fastgpx::LatLong min{std::get<0>(min_tuple), std::get<1>(min_tuple)};
fastgpx::LatLong max{std::get<0>(max_tuple), std::get<1>(max_tuple)};
return fastgpx::Bounds(min, max);
}),
nb::arg("min"), nb::arg("max"))
.def_rw("min", &fastgpx::Bounds::min)
.def_rw("max", &fastgpx::Bounds::max)
.def("is_empty", &fastgpx::Bounds::IsEmpty)The porting guide shows an example, but without arguments? From what I understand, when you create a custom ctor with nanobind you're given a pointer to some memory where you can use placement new. But where do you obtain arguments given to the custom ctor? |
Beta Was this translation helpful? Give feedback.
Answered by
thomthom
Nov 20, 2025
Replies: 1 comment 3 replies
-
|
I think I got it. The pre-allocated comes first, then any arguments comes afterwards: // Allow tuples instead of explicit LatLong objects.
.def(
"__init__",
[](fastgpx::Bounds* obj, std::tuple<double, double> min_tuple,
std::tuple<double, double> max_tuple) {
fastgpx::LatLong min{std::get<0>(min_tuple), std::get<1>(min_tuple)};
fastgpx::LatLong max{std::get<0>(max_tuple), std::get<1>(max_tuple)};
return new (obj) fastgpx::Bounds(min, max);
},
nb::arg("min"), nb::arg("max")) |
Beta Was this translation helpful? Give feedback.
3 replies
Answer selected by
thomthom
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment

I think I got it. The pre-allocated comes first, then any arguments comes afterwards: