@@ -1725,13 +1725,16 @@ T cast(const handle &handle) { return T(reinterpret_borrow<object>(handle)); }
1725
1725
1726
1726
// C++ type -> py::object
1727
1727
template <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,
1729
1729
handle parent = handle()) {
1730
+ using no_ref_T = typename std::remove_reference<T>::type;
1730
1731
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;
1732
1734
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));
1735
1738
}
1736
1739
1737
1740
template <typename T> T handle::cast () const { return pybind11::cast<T>(*this ); }
0 commit comments