diff --git a/include/pybind11/cast.h b/include/pybind11/cast.h index 62160a4d3..9584b02df 100644 --- a/include/pybind11/cast.h +++ b/include/pybind11/cast.h @@ -1235,7 +1235,7 @@ struct smart_holder_type_caster_load { return std::shared_ptr(void_ptr, convert_type(void_ptr.get())); } - template > + template std::unique_ptr loaded_as_unique_ptr(const char *context = "loaded_as_unique_ptr") { holder().template ensure_compatible_rtti_uqp_del(context); holder().ensure_use_count_1(context); diff --git a/include/pybind11/detail/smart_holder_poc.h b/include/pybind11/detail/smart_holder_poc.h index e46f2fb0d..d5c60403a 100644 --- a/include/pybind11/detail/smart_holder_poc.h +++ b/include/pybind11/detail/smart_holder_poc.h @@ -77,6 +77,12 @@ struct guarded_custom_deleter { } }; +template +inline bool is_std_default_delete(const std::type_info &rtti_deleter) { + return rtti_deleter == typeid(std::default_delete) + || rtti_deleter == typeid(std::default_delete); +} + struct smart_holder { const std::type_info *rtti_uqp_del; std::unique_ptr vptr_deleter_armed_flag_ptr; @@ -123,9 +129,7 @@ struct smart_holder { void ensure_compatible_rtti_uqp_del(const char *context) const { const std::type_info *rtti_requested = &typeid(D); if (!rtti_uqp_del) { - // IMPROVABLE: const-correctness. - if (!(*rtti_requested == typeid(std::default_delete) - || *rtti_requested == typeid(std::default_delete))) { + if (!is_std_default_delete(*rtti_requested)) { throw std::runtime_error(std::string("Missing unique_ptr deleter (") + context + ")."); } @@ -214,11 +218,11 @@ struct smart_holder { return raw_ptr; } - template > + template static smart_holder from_unique_ptr(std::unique_ptr &&unq_ptr) { smart_holder hld(true); hld.rtti_uqp_del = &typeid(D); - hld.vptr_is_using_builtin_delete = (*hld.rtti_uqp_del == typeid(std::default_delete)); + hld.vptr_is_using_builtin_delete = is_std_default_delete(*hld.rtti_uqp_del); if (hld.vptr_is_using_builtin_delete) { hld.vptr.reset(unq_ptr.get(), guarded_builtin_delete(hld.vptr_deleter_armed_flag_ptr.get()));