Skip to content

Commit 8e40e38

Browse files
authored
cast pointer to std::tuple and std::pair (#2334)
1 parent c51b3f4 commit 8e40e38

File tree

3 files changed

+18
-0
lines changed

3 files changed

+18
-0
lines changed

include/pybind11/cast.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1421,6 +1421,17 @@ template <template<typename...> class Tuple, typename... Ts> class tuple_caster
14211421
return cast_impl(std::forward<T>(src), policy, parent, indices{});
14221422
}
14231423

1424+
// copied from the PYBIND11_TYPE_CASTER macro
1425+
template <typename T>
1426+
static handle cast(T *src, return_value_policy policy, handle parent) {
1427+
if (!src) return none().release();
1428+
if (policy == return_value_policy::take_ownership) {
1429+
auto h = cast(std::move(*src), policy, parent); delete src; return h;
1430+
} else {
1431+
return cast(*src, policy, parent);
1432+
}
1433+
}
1434+
14241435
static constexpr auto name = _("Tuple[") + concat(make_caster<Ts>::name...) + _("]");
14251436

14261437
template <typename T> using cast_op_type = type;

tests/test_builtin_casters.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,12 +117,16 @@ TEST_SUBMODULE(builtin_casters, m) {
117117
return std::make_pair(RValueCaster{}, std::make_tuple(RValueCaster{}, std::make_pair(RValueCaster{}, RValueCaster{}))); });
118118
m.def("lvalue_nested", []() -> const decltype(lvnested) & { return lvnested; });
119119

120+
static std::pair<int, std::string> int_string_pair{2, "items"};
121+
m.def("int_string_pair", []() { return &int_string_pair; });
122+
120123
// test_builtins_cast_return_none
121124
m.def("return_none_string", []() -> std::string * { return nullptr; });
122125
m.def("return_none_char", []() -> const char * { return nullptr; });
123126
m.def("return_none_bool", []() -> bool * { return nullptr; });
124127
m.def("return_none_int", []() -> int * { return nullptr; });
125128
m.def("return_none_float", []() -> float * { return nullptr; });
129+
m.def("return_none_pair", []() -> std::pair<int,int> * { return nullptr; });
126130

127131
// test_none_deferred
128132
m.def("defer_none_cstring", [](char *) { return false; });

tests/test_builtin_casters.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,8 @@ def test_tuple(doc):
250250
assert m.rvalue_nested() == ("rvalue", ("rvalue", ("rvalue", "rvalue")))
251251
assert m.lvalue_nested() == ("lvalue", ("lvalue", ("lvalue", "lvalue")))
252252

253+
assert m.int_string_pair() == (2, "items")
254+
253255

254256
def test_builtins_cast_return_none():
255257
"""Casters produced with PYBIND11_TYPE_CASTER() should convert nullptr to None"""
@@ -258,6 +260,7 @@ def test_builtins_cast_return_none():
258260
assert m.return_none_bool() is None
259261
assert m.return_none_int() is None
260262
assert m.return_none_float() is None
263+
assert m.return_none_pair() is None
261264

262265

263266
def test_none_deferred():

0 commit comments

Comments
 (0)