@@ -84,6 +84,17 @@ class unique_ptr_with_addressof_operator {
84
84
};
85
85
PYBIND11_DECLARE_HOLDER_TYPE (T, unique_ptr_with_addressof_operator<T>);
86
86
87
+ // Simple custom holder that works like shared_ptr, but has a different memory layout
88
+ template <typename T>
89
+ class huge_shared_ptr {
90
+ uint64_t padding[10 ];
91
+ std::shared_ptr<T> impl;
92
+ public:
93
+ huge_shared_ptr ( ) = default ;
94
+ huge_shared_ptr (T* p) : impl(p) { }
95
+ T* get () const { return impl.get (); }
96
+ };
97
+ PYBIND11_DECLARE_HOLDER_TYPE (T, huge_shared_ptr<T>);
87
98
88
99
TEST_SUBMODULE (smart_ptr, m) {
89
100
@@ -142,6 +153,10 @@ TEST_SUBMODULE(smart_ptr, m) {
142
153
m.def (" print_myobject2_2" , [](std::shared_ptr<MyObject2> obj) { py::print (obj->toString ()); });
143
154
m.def (" print_myobject2_3" , [](const std::shared_ptr<MyObject2> &obj) { py::print (obj->toString ()); });
144
155
m.def (" print_myobject2_4" , [](const std::shared_ptr<MyObject2> *obj) { py::print ((*obj)->toString ()); });
156
+ // Using wrong holder type should raise a cast_error at runtime
157
+ m.def (" make_myobject2_3" , []() { return huge_shared_ptr<MyObject2>(new MyObject2 (9 )); });
158
+ m.def (" print_myobject2_5" , [](const huge_shared_ptr<MyObject2> &obj) { py::print (obj.get ()->toString ()); });
159
+ m.def (" print_myobject2_6" , [](const huge_shared_ptr<MyObject2> *obj) { py::print (obj->get ()->toString ()); });
145
160
146
161
// Object managed by a std::shared_ptr<>, additionally derives from std::enable_shared_from_this<>
147
162
class MyObject3 : public std ::enable_shared_from_this<MyObject3> {
0 commit comments