Add a polymorphic static assert when using an alias

An alias can be used for two main purposes: to override virtual methods,
and to add some extra data to a class needed for the pybind-wrapper.
Both of these absolutely require that the wrapped class be polymorphic
so that virtual dispatch and destruction, respectively, works.
This commit is contained in:
Jason Rhinelander 2017-06-12 21:48:36 -04:00
parent b4bf5ed575
commit 42e5ddc541
2 changed files with 4 additions and 1 deletions

View File

@ -950,6 +950,9 @@ public:
static_assert(detail::all_of<is_valid_class_option<options>...>::value, static_assert(detail::all_of<is_valid_class_option<options>...>::value,
"Unknown/invalid class_ template parameters provided"); "Unknown/invalid class_ template parameters provided");
static_assert(!has_alias || std::is_polymorphic<type>::value,
"Cannot use an alias class with a non-polymorphic type");
PYBIND11_OBJECT(class_, generic_type, PyType_Check) PYBIND11_OBJECT(class_, generic_type, PyType_Check)
template <typename... Extra> template <typename... Extra>

View File

@ -231,7 +231,7 @@ TEST_SUBMODULE(class_, m) {
bind_local<LocalExternal, 17>(m, "LocalExternal", py::module_local()); bind_local<LocalExternal, 17>(m, "LocalExternal", py::module_local());
} }
template <int N> class BreaksBase {}; template <int N> class BreaksBase { public: virtual ~BreaksBase() = default; };
template <int N> class BreaksTramp : public BreaksBase<N> {}; template <int N> class BreaksTramp : public BreaksBase<N> {};
// These should all compile just fine: // These should all compile just fine:
typedef py::class_<BreaksBase<1>, std::unique_ptr<BreaksBase<1>>, BreaksTramp<1>> DoesntBreak1; typedef py::class_<BreaksBase<1>, std::unique_ptr<BreaksBase<1>>, BreaksTramp<1>> DoesntBreak1;