Skip to content

Commit 6eb3cd8

Browse files
unique_ptr: Resolve RobotLocomotion/drake#8160
1 parent ac2205a commit 6eb3cd8

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

include/pybind11/cast.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1721,6 +1721,23 @@ struct move_only_holder_caster : type_caster_base<type> {
17211721
}
17221722

17231723
holder_type holder;
1724+
1725+
// N.B. We have to use this weird transaction because trying to introduce a
1726+
// destructor in `move_only_holder_caster` confuses the compiler, and makes
1727+
// it think it's copying this holder?
1728+
struct holder_transaction {
1729+
holder_type& holder;
1730+
~holder_transaction() {
1731+
if (holder) {
1732+
// If the argument was loaded into C++, but not transferred out,
1733+
// then this was most likely part of a failed overload in
1734+
// `argument_loader`. Transfer ownership back to Python.
1735+
move_only_holder_caster::cast(
1736+
std::move(holder), return_value_policy{}, handle{});
1737+
}
1738+
}
1739+
};
1740+
holder_transaction transaction{holder};
17241741
};
17251742

17261743
template <typename type, typename deleter>

0 commit comments

Comments
 (0)