此代码无法编译:
#include <QString>
/* relevant part:
struct QString
{
~QString() noexcept(false) {};
};
*/
class Base
{
public:
virtual ~Base() = default;
};
class Derived : public Base
{
QString string_;
};
int main()
{
return 0;
}错误是:
error: looser throw specifier for 'virtual Derived::~Derived()'
error: overriding 'virtual Base::~Base() noexcept (true)'我没有使用异常的经验,但我认为问题在于QString析构函数没有异常说明符,因此隐式创建的Derived::~Derived也没有异常说明符。这与作为noexcept(true)的隐式Base::~Base不兼容。
如果我排除QString或将其替换为一个带有noexcept(true)的类(比如std::string),代码就会编译。
起初,我认为可以通过将两个析构函数都声明为noexcept(false)来解决这个问题
virtual ~Base() noexcept(false) = default;
virtual ~Derived() noexcept(false) = default;但我得到的只是:
error: function 'virtual Base::~Base()' defaulted on its first declaration
with an exception-specification that differs from
the implicit declaration 'Base::~Base()'
error: looser throw specifier for 'virtual Derived::~Derived() noexcept (false)'
error: overriding 'virtual Base::~Base() noexcept (true)'我没有在代码中的任何地方使用异常,所以我正在寻找的东西只是一个“修复”。
发布于 2013-06-24 18:40:48
你似乎被你的QString给冲了,它指定了自己的数据类型。
我看不到简单的解决办法(除了使用合理的字符串):您可以随意指定您的基类dtor为noexcept(false),或者显式地派生dtor并表达noexcept(true)。(我不确定如果~QString真的抛出了,会不会有什么好的事情发生,但这会导致第一个转义路径)。
https://stackoverflow.com/questions/17273165
复制相似问题