c++中的松散抛出说明符

Looser Throw Specifier in C++

本文关键字:说明符 c++      更新时间:2023-10-16

这个错误是什么意思?我该怎么修理它?这是导致它的头代码:

class BadJumbleException : public exception {
public:
    BadJumbleException (const string& msg); // Constructor, accepts a string as the message
    string& what();                         // Returns the message string
private:
    string message;                         // Stores the exception message
};

源代码:

BadJumbleException::BadJumbleException (const string& m) : message(m) {}
string& BadJumbleException::what() { return message; }

编辑:这是错误:

松散抛出指定符'virtual BadJumbleException::~BadJumbleException()

在c++ 03中,per §18.6.1/5, std::exception有一个析构函数,它被声明为不能抛出异常(相反会引起编译错误)。

该语言要求,当从这样的类型派生时,您自己的析构函数必须具有相同的限制:

virtual BadJumbleException::~BadJumbleException() throw() {}
//                                                ^^^^^^^

这是因为重写函数可能没有松散抛出规范


在c++ 11中,std::exception::~exception而不是在库代码中显式标记为throw()(或noexcept)的,但是所有析构函数默认都是noexcept(true)

由于该规则将包含您的析构函数并允许您的程序编译,因此我得出结论,您的并没有真正编译为c++ 11。