You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The porting guide from pybind11 explains why nanobind doesn't have an equivalent of .def(py::init(<lambda>)):
Unfortunately, the implementation of this feature was quite complex and often required further internal calls to the move or copy constructor.
However, nanobind has something pybind11 didn't have: the ability to depend on C++17 features, including guaranteed copy elision. You can construct a T using a lambda-that-returns-a-T without doing any copies or moves at all, even if T isn't copyable or movable.
I'm wondering whether there might be appetite for accepting a PR that allows nb::class_<T>(...).def(nb::init(<lambda>)), only in those cases where <lambda> returns something that can direct-initialize a T. This would be intended as purely syntactic sugar for the placement-new formula, and would be expected to compile to an equivalent level of efficiency in most cases.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
The porting guide from pybind11 explains why nanobind doesn't have an equivalent of
.def(py::init(<lambda>))
:However, nanobind has something pybind11 didn't have: the ability to depend on C++17 features, including guaranteed copy elision. You can construct a T using a lambda-that-returns-a-T without doing any copies or moves at all, even if T isn't copyable or movable.
I'm wondering whether there might be appetite for accepting a PR that allows
nb::class_<T>(...).def(nb::init(<lambda>))
, only in those cases where<lambda>
returns something that can direct-initialize a T. This would be intended as purely syntactic sugar for the placement-new formula, and would be expected to compile to an equivalent level of efficiency in most cases.Beta Was this translation helpful? Give feedback.
All reactions