File tree Expand file tree Collapse file tree 2 files changed +33
-0
lines changed Expand file tree Collapse file tree 2 files changed +33
-0
lines changed Original file line number Diff line number Diff line change @@ -335,6 +335,28 @@ TEST_SUBMODULE(smart_ptr, m) {
335
335
return py::cast (std::move (obj));
336
336
});
337
337
338
+ class FirstT {};
339
+ py::class_<FirstT>(m, " FirstT" )
340
+ .def (py::init ());
341
+ class SecondT {};
342
+ py::class_<SecondT>(m, " SecondT" )
343
+ .def (py::init ());
344
+
345
+ m.def (" unique_ptr_overload" ,
346
+ [](std::unique_ptr<UniquePtrHeld> obj, FirstT) {
347
+ py::dict out;
348
+ out[" obj" ] = py::cast (std::move (obj));
349
+ out[" overload" ] = 1 ;
350
+ return out;
351
+ });
352
+ m.def (" unique_ptr_overload" ,
353
+ [](std::unique_ptr<UniquePtrHeld> obj, SecondT) {
354
+ py::dict out;
355
+ out[" obj" ] = py::cast (std::move (obj));
356
+ out[" overload" ] = 2 ;
357
+ return out;
358
+ });
359
+
338
360
// Ensure class is non-empty, so it's easier to detect double-free
339
361
// corruption. (If empty, this may be harder to see easily.)
340
362
struct SharedPtrHeld { int value = 10 ; };
Original file line number Diff line number Diff line change @@ -258,3 +258,14 @@ def test_unique_ptr_arg():
258
258
def test_unique_ptr_to_shared_ptr ():
259
259
obj = m .shared_ptr_held_in_unique_ptr ()
260
260
assert m .shared_ptr_held_func (obj )
261
+
262
+
263
+ def test_unique_ptr_overload_fail ():
264
+ obj = m .UniquePtrHeld (1 )
265
+ # These overloads pass ownership back to Python.
266
+ out = m .unique_ptr_overload (obj , m .FirstT ())
267
+ assert out ["obj" ] is obj
268
+ assert out ["overload" ] == 1
269
+ out = m .unique_ptr_overload (obj , m .SecondT ())
270
+ assert out ["obj" ] is obj
271
+ assert out ["overload" ] == 2
You can’t perform that action at this time.
0 commit comments