Skip to content

Commit 96ea0fa

Browse files
Merge pull request #28 from m-chaturvedi/drake_drake_fix_cast
cast: Ensure that pointers do not use rvalue / move overload
2 parents e4e3745 + b893bd3 commit 96ea0fa

File tree

3 files changed

+14
-1
lines changed

3 files changed

+14
-1
lines changed

include/pybind11/cast.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1970,7 +1970,7 @@ detail::enable_if_t<
19701970

19711971
template <typename T>
19721972
detail::enable_if_t<
1973-
std::is_rvalue_reference<T&&>::value && !detail::is_pyobject<detail::intrinsic_t<T>>::value, object>
1973+
std::is_rvalue_reference<T&&>::value && !std::is_pointer<T>::value && !detail::is_pyobject<detail::intrinsic_t<T>>::value, object>
19741974
cast(T&& value) {
19751975
// Have to use `pybind11::move` because some compilers might try to bind `move` to `std::move`...
19761976
return pybind11::move<T>(std::move(value));

tests/test_builtin_casters.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,4 +167,13 @@ TEST_SUBMODULE(builtin_casters, m) {
167167
py::object o = py::cast(v);
168168
return py::cast<void *>(o) == v;
169169
});
170+
171+
// For Drake issue: https://github.com/RobotLocomotion/drake/issues/9398
172+
m.def("test_pointer_caster", []() -> bool {
173+
UserType a;
174+
UserType *a_ptr = &a;
175+
py::object o = py::cast(&a); // Rvalue
176+
py::object o1 = py::cast(a_ptr); // Non-rvalue
177+
return (py::cast<UserType*>(o) == a_ptr && py::cast<UserType*>(o1) == a_ptr);
178+
});
170179
}

tests/test_builtin_casters.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,3 +340,7 @@ def test_int_long():
340340

341341
def test_void_caster_2():
342342
assert m.test_void_caster()
343+
344+
345+
def test_pointer_caster():
346+
assert m.test_pointer_caster()

0 commit comments

Comments
 (0)