Skip to content

Commit de49686

Browse files
author
Mmanu Chaturvedi
committed
Fix cast and add test
1 parent e4e3745 commit de49686

File tree

3 files changed

+25
-1
lines changed

3 files changed

+25
-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: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,4 +167,24 @@ TEST_SUBMODULE(builtin_casters, m) {
167167
py::object o = py::cast(v);
168168
return py::cast<void *>(o) == v;
169169
});
170+
class A {
171+
public:
172+
std::string s;
173+
int i;
174+
std::pair<std::string, int> P;
175+
};
176+
177+
py::class_<A>(m, "A")
178+
.def(py::init());
179+
180+
m.def("test_pointer_caster", []() -> bool {
181+
A a;
182+
a.s = "String 1";
183+
a.i = 100;
184+
a.P = std::pair<std::string, int>("String 2", 10);
185+
A *a_ptr = &a;
186+
py::object o = py::cast(&a);
187+
py::object o1 = py::cast(a_ptr);
188+
return (py::cast<A*>(o) == a_ptr && py::cast<A*>(o1) == a_ptr);
189+
});
170190
}

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)