Skip to content

Commit 501135f

Browse files
committed
Add static_assert to holder casters
The holder casters assume but don't check that a `holder<type>`'s `type` is really a `type_caster_base<type>`; this adds a static_assert to make sure this is really the case, to turn things like `std::shared_ptr<array>` into a compilation failure. Fixes #785
1 parent d994db2 commit 501135f

File tree

1 file changed

+5
-0
lines changed

1 file changed

+5
-0
lines changed

include/pybind11/cast.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -902,6 +902,8 @@ template <typename type, typename holder_type>
902902
struct copyable_holder_caster : public type_caster_base<type> {
903903
public:
904904
using base = type_caster_base<type>;
905+
static_assert(std::is_base_of<base, type_caster<type>>::value,
906+
"Holder classes are only supported for custom types");
905907
using base::base;
906908
using base::cast;
907909
using base::typeinfo;
@@ -1018,6 +1020,9 @@ class type_caster<std::shared_ptr<T>> : public copyable_holder_caster<T, std::sh
10181020

10191021
template <typename type, typename holder_type>
10201022
struct move_only_holder_caster {
1023+
static_assert(std::is_base_of<type_caster_base<type>, type_caster<type>>::value,
1024+
"Holder classes are only supported for custom types");
1025+
10211026
static handle cast(holder_type &&src, return_value_policy, handle) {
10221027
auto *ptr = holder_helper<holder_type>::get(src);
10231028
return type_caster_base<type>::cast_holder(ptr, &src);

0 commit comments

Comments
 (0)