clang -Wnon-virtual-dtor compatibility (#2626)

* Adding missing virtual destructors, to silence clang -Wnon-virtual-dtor warnings.

Tested with clang version 9.0.1-12 under an Ubuntu-like OS.
Originally discovered in the Google-internal environment.

* adding -Wnon-virtual-dtor for GNU|Intel|Clang
This commit is contained in:
Ralf W. Grosse-Kunstleve 2020-10-29 09:12:14 -04:00 committed by GitHub
parent f2e799863b
commit 8290a5a0da
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 21 additions and 3 deletions

View File

@ -243,8 +243,15 @@ function(pybind11_enable_warnings target_name)
if(MSVC) if(MSVC)
target_compile_options(${target_name} PRIVATE /W4) target_compile_options(${target_name} PRIVATE /W4)
elseif(CMAKE_CXX_COMPILER_ID MATCHES "(GNU|Intel|Clang)" AND NOT PYBIND11_CUDA_TESTS) elseif(CMAKE_CXX_COMPILER_ID MATCHES "(GNU|Intel|Clang)" AND NOT PYBIND11_CUDA_TESTS)
target_compile_options(${target_name} PRIVATE -Wall -Wextra -Wconversion -Wcast-qual target_compile_options(
-Wdeprecated -Wundef) ${target_name}
PRIVATE -Wall
-Wextra
-Wconversion
-Wcast-qual
-Wdeprecated
-Wundef
-Wnon-virtual-dtor)
endif() endif()
if(PYBIND11_WERROR) if(PYBIND11_WERROR)

View File

@ -117,7 +117,11 @@ TEST_SUBMODULE(callbacks, m) {
} }
}); });
class AbstractBase { public: virtual unsigned int func() = 0; }; class AbstractBase {
public:
virtual ~AbstractBase() = default;
virtual unsigned int func() = 0;
};
m.def("func_accepting_func_accepting_base", [](std::function<double(AbstractBase&)>) { }); m.def("func_accepting_func_accepting_base", [](std::function<double(AbstractBase&)>) { });
struct MovableObject { struct MovableObject {

View File

@ -32,6 +32,13 @@ class MyException3 {
public: public:
explicit MyException3(const char * m) : message{m} {} explicit MyException3(const char * m) : message{m} {}
virtual const char * what() const noexcept {return message.c_str();} virtual const char * what() const noexcept {return message.c_str();}
// Rule of 5 BEGIN: to preempt compiler warnings.
MyException3(const MyException3&) = default;
MyException3(MyException3&&) = default;
MyException3& operator=(const MyException3&) = default;
MyException3& operator=(MyException3&&) = default;
virtual ~MyException3() = default;
// Rule of 5 END.
private: private:
std::string message = ""; std::string message = "";
}; };