Skip to content

Commit 4697149

Browse files
rwgkwjakob
authored andcommitted
Allows users to specialize polymorphic_type_hook with std::enable_if.
Currently user specializations of the form template <typename itype> struct polymorphic_type_hook<itype, std::enable_if_t<...>> { ... }; will fail if itype is also polymorphic, because the existing specialization will also be enabled, which leads to 2 equally viable candidates. With this change, user provided specializations have higher priority than the built in specialization for polymorphic types.
1 parent 0234871 commit 4697149

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

include/pybind11/cast.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -835,19 +835,25 @@ NAMESPACE_END(detail)
835835
// You may specialize polymorphic_type_hook yourself for types that want to appear
836836
// polymorphic to Python but do not use C++ RTTI. (This is a not uncommon pattern
837837
// in performance-sensitive applications, used most notably in LLVM.)
838+
//
839+
// polymorphic_type_hook_base allows users to specialize polymorphic_type_hook with
840+
// std::enable_if. User provided specializations will always have higher priority than
841+
// the default implementation and specialization provided in polymorphic_type_hook_base.
838842
template <typename itype, typename SFINAE = void>
839-
struct polymorphic_type_hook
843+
struct polymorphic_type_hook_base
840844
{
841845
static const void *get(const itype *src, const std::type_info*&) { return src; }
842846
};
843847
template <typename itype>
844-
struct polymorphic_type_hook<itype, detail::enable_if_t<std::is_polymorphic<itype>::value>>
848+
struct polymorphic_type_hook_base<itype, detail::enable_if_t<std::is_polymorphic<itype>::value>>
845849
{
846850
static const void *get(const itype *src, const std::type_info*& type) {
847851
type = src ? &typeid(*src) : nullptr;
848852
return dynamic_cast<const void*>(src);
849853
}
850854
};
855+
template <typename itype, typename SFINAE = void>
856+
struct polymorphic_type_hook : public polymorphic_type_hook_base<itype> {};
851857

852858
NAMESPACE_BEGIN(detail)
853859

0 commit comments

Comments
 (0)