Add static_asserts to enforce that py::smart_holder is combined with py::trampoline_self_life_support (#5633)

* Strictly enforce: trampoline must inherit from trampoline_self_life_support when used in combination with smart_holder

* Simplify test_class_sh_trampoline_basic.cpp,py (only one Abase is needed now)

* Replace obsolete sophisticated `throw value_error()` with a simple `assert()`

* Strictly enforce: trampoline should inherit from trampoline_self_life_support only if used in combination with smart_holder

* Resolve clang-tidy error

```
/__w/pybind11/pybind11/tests/test_class_sh_trampoline_basic.cpp:35:46: error: the parameter 'obj' is copied for each invocation but only used as a const reference; consider making it a const reference [performance-unnecessary-value-param,-warnings-as-errors]
   35 | int AddInCppSharedPtr(std::shared_ptr<Abase> obj, int other_val) {
      |                                              ^
      |                       const                 &
```

* Disable new static_assert if PYBIND11_RUN_TESTING_WITH_SMART_HOLDER_AS_DEFAULT_BUT_NEVER_USE_IN_PRODUCTION_PLEASE is defined.
This commit is contained in:
Ralf W. Grosse-Kunstleve
2025-04-30 22:12:13 -07:00
committed by GitHub
parent c7f3460f18
commit c630e22c1c
7 changed files with 56 additions and 87 deletions

View File

@@ -64,7 +64,7 @@ struct with_alias {
with_alias &operator=(const with_alias &) = default;
with_alias &operator=(with_alias &&) = default;
};
struct with_alias_alias : with_alias {};
struct with_alias_alias : with_alias, py::trampoline_self_life_support {};
struct sddwaa : std::default_delete<with_alias_alias> {};
} // namespace class_sh_factory_constructors