This code compiles on gcc and msvc but not with clang from C++20 on:
#include <type_traits>
class IBase {
protected:
IBase() noexcept = default;
public:
virtual ~IBase() noexcept = default;
};
class Derived : public IBase {
public:
virtual ~Derived() noexcept(
std::is_nothrow_destructible<IBase>::value) override = default;
};
Up to C++17 included, gcc, msvc and clang compile this code but from C++20, clang rejects it with error: exception specification is not available until end of class definition
.
Is it a clang bug or, on the contrary, a change in exception specification in C++20? How can I fix this code?
[EDIT] Based on the comments, here is a "more minimal reproducible example":
class IBase {
public:
virtual ~IBase() noexcept = default;
};
class Derived : public IBase {
public:
~Derived() noexcept(true) override = default;
};
Notice that the inheritance and both = default
seem necessary to trigger the error.
How can I fix this code?
Although I don't know if this is a clang bug or not but I do know a workaround if you really want to =default
the dtor.
In particular, you can =default
outside the Derived
class as shown below.
class Derived : public IBase {
public:
~Derived() noexcept(true) ;
};
//default outside the class
Derived::~Derived() noexcept(true)= default;
int main() {
}