mirror of
https://github.com/pybind/pybind11.git
synced 2024-11-11 08:03:55 +00:00
ed14879a19
Currently pybind11 always translates values returned by Python functions invoked from C++ code by copying, even when moving is feasible--and, more importantly, even when moving is required. The first, and relatively minor, concern is that moving may be considerably more efficient for some types. The second problem, however, is more serious: there's currently no way python code can return a non-copyable type to C++ code. I ran into this while trying to add a PYBIND11_OVERLOAD of a virtual method that returns just such a type: it simply fails to compile because this: overload = ... overload(args).template cast<ret_type>(); involves a copy: overload(args) returns an object instance, and the invoked object::cast() loads the returned value, then returns a copy of the loaded value. We can, however, safely move that returned value *if* the object has the only reference to it (i.e. if ref_count() == 1) and the object is itself temporary (i.e. if it's an rvalue). This commit does that by adding an rvalue-qualified object::cast() method that allows the returned value to be move-constructed out of the stored instance when feasible. This basically comes down to three cases: - For objects that are movable but not copyable, we always try the move, with a runtime exception raised if this would involve moving a value with multiple references. - When the type is both movable and non-trivially copyable, the move happens only if the invoked object has a ref_count of 1, otherwise the object is copied. (Trivially copyable types are excluded from this case because they are typically just collections of primitive types, which can be copied just as easily as they can be moved.) - Non-movable and trivially copy constructible objects are simply copied. This also adds examples to example-virtual-functions that shows both a non-copyable object and a movable/copyable object in action: the former raises an exception if returned while holding a reference, the latter invokes a move constructor if unreferenced, or a copy constructor if referenced. Basically this allows code such as: class MyClass(Pybind11Class): def somemethod(self, whatever): mt = MovableType(whatever) # ... return mt which allows the MovableType instance to be returned to the C++ code via its move constructor. Of course if you attempt to violate this by doing something like: self.value = MovableType(whatever) return self.value you get an exception--but right now, the pybind11-side of that code won't compile at all. |
||
---|---|---|
.. | ||
CMakeLists.txt | ||
eigen.cpp | ||
eigen.py | ||
eigen.ref | ||
example-arg-keywords-and-defaults.cpp | ||
example-arg-keywords-and-defaults.py | ||
example-arg-keywords-and-defaults.ref | ||
example-buffers.cpp | ||
example-buffers.py | ||
example-buffers.ref | ||
example-callbacks.cpp | ||
example-callbacks.py | ||
example-callbacks.ref | ||
example-constants-and-functions.cpp | ||
example-constants-and-functions.py | ||
example-constants-and-functions.ref | ||
example-custom-exceptions.cpp | ||
example-custom-exceptions.py | ||
example-custom-exceptions.ref | ||
example-eval_call.py | ||
example-eval.cpp | ||
example-eval.py | ||
example-eval.ref | ||
example-inheritance.cpp | ||
example-inheritance.py | ||
example-inheritance.ref | ||
example-keep-alive.cpp | ||
example-keep-alive.py | ||
example-keep-alive.ref | ||
example-methods-and-attributes.cpp | ||
example-methods-and-attributes.py | ||
example-methods-and-attributes.ref | ||
example-modules.cpp | ||
example-modules.py | ||
example-modules.ref | ||
example-numpy-vectorize.cpp | ||
example-numpy-vectorize.py | ||
example-numpy-vectorize.ref | ||
example-opaque-types.cpp | ||
example-opaque-types.py | ||
example-opaque-types.ref | ||
example-operator-overloading.cpp | ||
example-operator-overloading.py | ||
example-operator-overloading.ref | ||
example-pickling.cpp | ||
example-pickling.py | ||
example-pickling.ref | ||
example-python-types.cpp | ||
example-python-types.py | ||
example-python-types.ref | ||
example-sequences-and-iterators.cpp | ||
example-sequences-and-iterators.py | ||
example-sequences-and-iterators.ref | ||
example-smart-ptr.cpp | ||
example-smart-ptr.py | ||
example-smart-ptr.ref | ||
example-stl-binder-vector.cpp | ||
example-stl-binder-vector.py | ||
example-stl-binder-vector.ref | ||
example-virtual-functions.cpp | ||
example-virtual-functions.py | ||
example-virtual-functions.ref | ||
example.cpp | ||
example.h | ||
issues.cpp | ||
issues.py | ||
issues.ref | ||
object.h | ||
run_test.py |