-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Description
When defining a holder type that can be initialized from a raw pointer T*
(https://pybind11.readthedocs.io/en/stable/advanced/smart_ptrs.html), pybind11 conventionally assumes that there is a constructor Holder(T*)
which will let it steal a raw pointer reference to initialize into the older. However, sometimes, you want to define a holder type where the raw pointer constructor doesn't have the correct semantics (e.g., std::shared_ptr
) or you want to omit the raw pointer constructor and explicitly require users to say if they want to steal or borrow from the passed in raw pointer.
Unfortunately, there is no way to currently do this; you must define another Wrapper class to work around this problem. Here is an example from our codebase, where c10::intrusive_ptr
is a holder type but it doesn't support construction from raw pointer (instead it's a static method called reclaim) and so I have to write this boilerplate:
template <typename T>
class IntrusivePtrOkForPybind11 {
c10::intrusive_ptr<T> impl_;
public:
IntrusivePtrOkForPybind11() = default;
IntrusivePtrOkForPybind11(const IntrusivePtrNoGilDestructor&) = default;
IntrusivePtrOkForPybind11(IntrusivePtrNoGilDestructor&&) = default;
IntrusivePtrOkForPybind11& operator=(const IntrusivePtrOkForPybind11&) = default;
IntrusivePtrOkForPybind11& operator=(IntrusivePtrOkForPybind11&&) = default;
IntrusivePtrOkForPybind11(c10::intrusive_ptr<T> impl) : impl_(std::move(impl)) {}
IntrusivePtrOkForPybind11(T* impl) : impl_(c10::intrusive_ptr<T>::reclaim(impl)) {}
T& operator*() const noexcept { return *impl_; }
T* operator->() const noexcept { return impl_.get(); }
T* get() const noexcept { return impl_.get(); }
void reset() noexcept { impl_.reset(); }
operator bool() const noexcept {
return impl_;
}
};
It would be nice if there was some way to configure similar logic to enable_shared_from_this for our own type so we can bypass the raw pointer constructor entirely.