diff --git a/tests/test_native_enum.cpp b/tests/test_native_enum.cpp index 1a4c3180c7..8ed3e40acb 100644 --- a/tests/test_native_enum.cpp +++ b/tests/test_native_enum.cpp @@ -45,11 +45,13 @@ struct is_proto_enum : std::true_type {}; PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE) PYBIND11_NAMESPACE_BEGIN(detail) +// Negate this condition to demonstrate "ambiguous template instantiation" compilation error: +#if defined(PYBIND11_HAS_NATIVE_ENUM) template struct type_caster_enum_type_enabled< ProtoEnumType, - detail::enable_if_t::value>> : std::false_type { -}; + enable_if_t::value>> : std::false_type {}; +#endif // https://github.com/pybind/pybind11_protobuf/blob/a50899c2eb604fc5f25deeb8901eff6231b8b3c0/pybind11_protobuf/enum_type_caster.h#L101-L105 template diff --git a/tests/test_smart_ptr.cpp b/tests/test_smart_ptr.cpp index 7c9f35371f..19b7b0d80e 100644 --- a/tests/test_smart_ptr.cpp +++ b/tests/test_smart_ptr.cpp @@ -290,6 +290,72 @@ PYBIND11_DECLARE_HOLDER_TYPE(T, custom_unique_ptr) PYBIND11_DECLARE_HOLDER_TYPE(T, shared_ptr_with_addressof_operator) PYBIND11_DECLARE_HOLDER_TYPE(T, unique_ptr_with_addressof_operator) +namespace holder_caster_traits_test { +struct example_base {}; +} // namespace holder_caster_traits_test + +PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE) +PYBIND11_NAMESPACE_BEGIN(detail) + +// Negate this condition to demonstrate "ambiguous template instantiation" compilation error: +#if defined(PYBIND11_HAS_INTERNALS_WITH_SMART_HOLDER_SUPPORT) +template +struct copyable_holder_caster_shared_ptr_with_smart_holder_support_enabled< + ExampleType, + enable_if_t::value>> + : std::false_type {}; +#endif + +template +struct copyable_holder_caster< + ExampleType, + HolderType, + enable_if_t::value>> { + static constexpr auto name = const_name(); + + static handle cast(const HolderType &, return_value_policy, handle) { + return str("copyable_holder_caster_traits_test").release(); + } +}; + +// Negate this condition to demonstrate "ambiguous template instantiation" compilation error: +#if defined(PYBIND11_HAS_INTERNALS_WITH_SMART_HOLDER_SUPPORT) +template +struct move_only_holder_caster_unique_ptr_with_smart_holder_support_enabled< + ExampleType, + enable_if_t::value>> + : std::false_type {}; +#endif + +template +struct move_only_holder_caster< + ExampleType, + HolderType, + enable_if_t::value>> { + static constexpr auto name = const_name(); + + static handle cast(const HolderType &, return_value_policy, handle) { + return str("move_only_holder_caster_traits_test").release(); + } +}; + +PYBIND11_NAMESPACE_END(detail) +PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE) + +namespace holder_caster_traits_test { + +struct example_drvd : example_base {}; + +void wrap(py::module_ &m) { + m.def("return_std_shared_ptr_example_drvd", + // NOLINTNEXTLINE(modernize-make-shared) + []() { return std::shared_ptr(new example_drvd()); }); + m.def("return_std_unique_ptr_example_drvd", + []() { return std::unique_ptr(new example_drvd()); }); +} + +} // namespace holder_caster_traits_test + TEST_SUBMODULE(smart_ptr, m) { // Please do not interleave `struct` and `class` definitions with bindings code, // but implement `struct`s and `class`es in the anonymous namespace above. @@ -482,4 +548,6 @@ TEST_SUBMODULE(smart_ptr, m) { .def(py::init<>()) .def_readwrite("ptr", &ContainerUsingPrivateESFT::ptr); // <- access ESFT through shared_ptr + + holder_caster_traits_test::wrap(m); } diff --git a/tests/test_smart_ptr.py b/tests/test_smart_ptr.py index 0b6f9b57a2..3c20458755 100644 --- a/tests/test_smart_ptr.py +++ b/tests/test_smart_ptr.py @@ -338,3 +338,15 @@ def test_private_esft_tolerance(): _ = c.ptr # getattr with pytest.raises(TypeError): c.ptr = None # setattr + + +def test_copyable_holder_caster_shared_ptr_with_smart_holder_support_enabled(): + assert ( + m.return_std_shared_ptr_example_drvd() == "copyable_holder_caster_traits_test" + ) + + +def test_move_only_holder_caster_shared_ptr_with_smart_holder_support_enabled(): + assert ( + m.return_std_unique_ptr_example_drvd() == "move_only_holder_caster_traits_test" + )