C++中的Looser Throw Specifier

use*_*889 11 c++ c++11

这个错误是什么意思?我该如何解决?这是导致它的标头代码:

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
};
Run Code Online (Sandbox Code Playgroud)

这是源代码:

BadJumbleException::BadJumbleException (const string& m) : message(m) {}
string& BadJumbleException::what() { return message; }
Run Code Online (Sandbox Code Playgroud)

编辑:这是错误:

'virtual BadJumbleException :: ~BadJumbleException()的松散抛出说明符

Lig*_*ica 25

在C++ 03中,根据§18.6.1/ 5,std::exception有一个析构函数,声明这样就不会抛出异常(反而会引起编译错误).

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

virtual BadJumbleException::~BadJumbleException() throw() {}
//                                                ^^^^^^^
Run Code Online (Sandbox Code Playgroud)

这是因为重写函数可能没有更宽松的抛出规范.


在C++ 11,std::exception::~exception标记的throw()(或noexcept明确地在库中的代码),但是所有的析构函数是noexcept(true)默认情况下.

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

  • 简而言之,这个错误是如何修复的?为什么这段代码在其他一些项目中有效,而在这个项目中无效? (2认同)