Skip to content

Commit b43f2b1

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

1 file changed

Lines changed: 18 additions & 0 deletions

File tree

include/pybind11/cast.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1721,6 +1721,24 @@ 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 `holder_transaction` struct because
1726+
// trying to introduce a destructor in `move_only_holder_caster` confuses
1727+
// the compiler, and makes it think it's copying the caster...
1728+
struct holder_transaction {
1729+
holder_type& holder;
1730+
holder_transaction() = default;
1731+
~holder_transaction() {
1732+
if (holder) {
1733+
// If the argument was loaded into C++, but not transferred out,
1734+
// then this was most likely part of a failed overload in
1735+
// `argument_loader`. Transfer ownership back to Python.
1736+
move_only_holder_caster::cast(
1737+
std::move(holder), return_value_policy{}, handle{});
1738+
}
1739+
}
1740+
};
1741+
holder_transaction transaction{holder};
17241742
};
17251743

17261744
template <typename type, typename deleter>

0 commit comments

Comments
 (0)