Skip to content

Commit

Permalink
Add static_assert to holder casters
Browse files Browse the repository at this point in the history
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 pybind#785
  • Loading branch information
jagerman committed Apr 7, 2017
1 parent db20095 commit a5f120f
Showing 1 changed file with 5 additions and 0 deletions.
5 changes: 5 additions & 0 deletions include/pybind11/cast.h
Original file line number Diff line number Diff line change
Expand Up @@ -902,6 +902,8 @@ template <typename type, typename holder_type>
struct copyable_holder_caster : public type_caster_base<type> {
public:
using base = type_caster_base<type>;
static_assert(std::is_base_of<base, type_caster<type>>::value,
"Holder classes are only supported for custom types");
using base::base;
using base::cast;
using base::typeinfo;
Expand Down Expand Up @@ -1018,6 +1020,9 @@ class type_caster<std::shared_ptr<T>> : public copyable_holder_caster<T, std::sh

template <typename type, typename holder_type>
struct move_only_holder_caster {
static_assert(std::is_base_of<type_caster_base<type>, type_caster<type>>::value,
"Holder classes are only supported for custom types");

static handle cast(holder_type &&src, return_value_policy, handle) {
auto *ptr = holder_helper<holder_type>::get(src);
return type_caster_base<type>::cast_holder(ptr, &src);
Expand Down

0 comments on commit a5f120f

Please sign in to comment.