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