@@ -1725,13 +1725,16 @@ T cast(const handle &handle) { return T(reinterpret_borrow<object>(handle)); }
17251725
17261726// C++ type -> py::object
17271727template <typename T, detail::enable_if_t <!detail::is_pyobject<T>::value, int > = 0 >
1728- object cast (const T &value, return_value_policy policy = return_value_policy::automatic_reference,
1728+ object cast (T & &value, return_value_policy policy = return_value_policy::automatic_reference,
17291729 handle parent = handle()) {
1730+ using no_ref_T = typename std::remove_reference<T>::type;
17301731 if (policy == return_value_policy::automatic)
1731- policy = std::is_pointer<T>::value ? return_value_policy::take_ownership : return_value_policy::copy;
1732+ policy = std::is_pointer<no_ref_T>::value ? return_value_policy::take_ownership :
1733+ std::is_lvalue_reference<T>::value ? return_value_policy::copy : return_value_policy::move;
17321734 else if (policy == return_value_policy::automatic_reference)
1733- policy = std::is_pointer<T>::value ? return_value_policy::reference : return_value_policy::copy;
1734- return reinterpret_steal<object>(detail::make_caster<T>::cast (value, policy, parent));
1735+ policy = std::is_pointer<no_ref_T>::value ? return_value_policy::reference :
1736+ std::is_lvalue_reference<T>::value ? return_value_policy::copy : return_value_policy::move;
1737+ return reinterpret_steal<object>(detail::make_caster<T>::cast (std::forward<T>(value), policy, parent));
17351738}
17361739
17371740template <typename T> T handle::cast () const { return pybind11::cast<T>(*this ); }
0 commit comments