|
3 | 3 | // https://github.com/pybind/pybind11/blob/30eb39ed79d1e2eeff15219ac00773034300a5e6/include/pybind11/cast.h#L235
|
4 | 4 | // `return reinterpret_cast<H &>(vh[1]);`
|
5 | 5 | // indirectly casts a `shared_ptr<drvd>` reference to a `shared_ptr<base>`.
|
6 |
| -// `test_smart_ptr_private_first_base.py` fails with an AssertionError and |
7 |
| -// a subsequent Segmentation Fault (Linux, clang++ -std=c++17). |
| 6 | +// Similarly: |
| 7 | +// https://github.com/pybind/pybind11/blob/30eb39ed79d1e2eeff15219ac00773034300a5e6/include/pybind11/pybind11.h#L1505 |
| 8 | +// `init_holder(inst, v_h, (const holder_type *) holder_ptr, v_h.value_ptr<type>());` |
| 9 | +// explictly casts a `shared_ptr<base>` reference to a `shared_ptr<drvd>`. |
| 10 | +// Both tests in `test_smart_ptr_private_first_base.py` fail with a |
| 11 | +// Segmentation Fault (Linux, clang++ -std=c++17). |
8 | 12 |
|
9 | 13 | #include <memory>
|
10 | 14 |
|
@@ -33,14 +37,21 @@ inline std::shared_ptr<drvd> make_shared_drvd() {
|
33 | 37 | return std::shared_ptr<drvd>(new drvd);
|
34 | 38 | }
|
35 | 39 |
|
| 40 | +inline std::shared_ptr<base> make_shared_drvd_up_cast() { |
| 41 | + return std::shared_ptr<base>(new drvd); |
| 42 | +} |
| 43 | + |
36 | 44 | inline int pass_shared_base(std::shared_ptr<base> b) { return b->id(); }
|
| 45 | +inline int pass_shared_drvd(std::shared_ptr<drvd> d) { return d->id(); } |
37 | 46 |
|
38 | 47 | TEST_SUBMODULE(smart_ptr_private_first_base, m) {
|
39 | 48 | py::class_<base, std::shared_ptr<base>>(m, "base");
|
40 | 49 | py::class_<drvd, base, std::shared_ptr<drvd>>(m, "drvd");
|
41 | 50 |
|
42 | 51 | m.def("make_shared_drvd", make_shared_drvd);
|
| 52 | + m.def("make_shared_drvd_up_cast", make_shared_drvd_up_cast); |
43 | 53 | m.def("pass_shared_base", pass_shared_base);
|
| 54 | + m.def("pass_shared_drvd", pass_shared_drvd); |
44 | 55 | }
|
45 | 56 |
|
46 | 57 | } // namespace smart_ptr_private_first_base
|
|
0 commit comments