Setting record.default_holder correctly for PYBIND11_USE_SMART_HOLDER_AS_DEFAULT.

With this test_class.cpp builds and even mostly runs, except
`test_multiple_instances_with_same_pointer`, which segfaults because it is
using a `unique_ptr` holder but `smart_holder` `type_caster`.

Also adding `static_assert`s to generate build errors for such situations,
but guarding with `#if 0` to first pivot to test_factory_constructors.cpp.
This commit is contained in:
Ralf W. Grosse-Kunstleve 2021-01-30 01:02:36 -08:00
parent f373e3d619
commit b9eb2a46c4

View File

@ -1294,7 +1294,16 @@ public:
record.holder_size = sizeof(holder_type);
record.init_instance = init_instance;
record.dealloc = dealloc;
#ifndef PYBIND11_USE_SMART_HOLDER_AS_DEFAULT
record.default_holder = detail::is_instantiation<std::unique_ptr, holder_type>::value;
#else
record.default_holder = std::is_same<holder_type, smart_holder>::value;
#if 0
static_assert(!(detail::is_instantiation<std::unique_ptr, holder_type>::value && detail::is_smart_holder_type_caster<type>::value));
static_assert(!(detail::is_instantiation<std::shared_ptr, holder_type>::value && detail::is_smart_holder_type_caster<type>::value));
static_assert(detail::is_smart_holder_type_caster<type>::value == std::is_same<holder_type, smart_holder>::value);
#endif
#endif
set_operator_new<type>(&record);