From 42e5ddc541f474d0f652528c0cbdcb4bbd1e9943 Mon Sep 17 00:00:00 2001 From: Jason Rhinelander Date: Mon, 12 Jun 2017 21:48:36 -0400 Subject: [PATCH] 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. --- include/pybind11/pybind11.h | 3 +++ tests/test_class.cpp | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/include/pybind11/pybind11.h b/include/pybind11/pybind11.h index bd0ee2733..5689d5a3c 100644 --- a/include/pybind11/pybind11.h +++ b/include/pybind11/pybind11.h @@ -950,6 +950,9 @@ public: static_assert(detail::all_of...>::value, "Unknown/invalid class_ template parameters provided"); + static_assert(!has_alias || std::is_polymorphic::value, + "Cannot use an alias class with a non-polymorphic type"); + PYBIND11_OBJECT(class_, generic_type, PyType_Check) template diff --git a/tests/test_class.cpp b/tests/test_class.cpp index 5860b741e..670949f80 100644 --- a/tests/test_class.cpp +++ b/tests/test_class.cpp @@ -231,7 +231,7 @@ TEST_SUBMODULE(class_, m) { bind_local(m, "LocalExternal", py::module_local()); } -template class BreaksBase {}; +template class BreaksBase { public: virtual ~BreaksBase() = default; }; template class BreaksTramp : public BreaksBase {}; // These should all compile just fine: typedef py::class_, std::unique_ptr>, BreaksTramp<1>> DoesntBreak1;