Skip to content

Address #1138 when mixing holder_type's upon casting an object. #1139

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions include/pybind11/cast.h
Original file line number Diff line number Diff line change
Expand Up @@ -1367,6 +1367,12 @@ template <typename... Ts> class type_caster<std::tuple<Ts...>>
template <typename T>
struct holder_helper {
static auto get(const T &p) -> decltype(p.get()) { return p.get(); }

// Specialize move-only holder semantics to address #1138.
template <typename U = T>
static auto get(T &&p, enable_if_t<!is_copy_constructible<U>::value>* = nullptr) -> decltype(p.get()) {
return p.release();
}
};

/// Type caster for holder types like std::shared_ptr, etc.
Expand Down Expand Up @@ -1456,8 +1462,11 @@ struct move_only_holder_caster {
"Holder classes are only supported for custom types");

static handle cast(holder_type &&src, return_value_policy, handle) {
auto *ptr = holder_helper<holder_type>::get(src);
return type_caster_base<type>::cast_holder(ptr, &src);
// Move `src` so that `holder_helper<>::get()` can call `release` if need be.
// That way, if we mix `holder_type`s, we don't have to worry about `existing_holder`
// from being mistakenly reinterpret_cast'd to `shared_ptr<type>` (#1138).
auto *ptr = holder_helper<holder_type>::get(std::move(src));
return type_caster_base<type>::cast_holder(ptr, nullptr);
}
static constexpr auto name = type_caster_base<type>::name;
};
Expand Down
5 changes: 3 additions & 2 deletions tests/test_smart_ptr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ template <typename T> class huge_unique_ptr {
uint64_t padding[10];
public:
huge_unique_ptr(T *p) : ptr(p) {};
T *get() { return ptr.get(); }
T *get() const { return ptr.get(); }
T* release() { return ptr.release(); }
};
PYBIND11_DECLARE_HOLDER_TYPE(T, huge_unique_ptr<T>);

Expand All @@ -51,7 +52,7 @@ class custom_unique_ptr {
public:
custom_unique_ptr(T* p) : impl(p) { }
T* get() const { return impl.get(); }
T* release_ptr() { return impl.release(); }
T* release() { return impl.release(); }
};
PYBIND11_DECLARE_HOLDER_TYPE(T, custom_unique_ptr<T>);

Expand Down