C++标准声明了以下关于具有异常规范的虚函数:
如果虚函数具有异常规范,则任何在任何派生类中覆盖该虚函数的函数的所有声明(包括定义)都只允许基类虚函数的异常规范所允许的异常(C +) +03§15.4/ 3).
因此,以下是不正确的:
struct B {
virtual void f() throw() { } // allows no exceptions
};
struct D : B {
virtual void f() { } // allows all exceptions
};
Run Code Online (Sandbox Code Playgroud)
(1)此规则是否适用于析构函数?也就是说,以下是否良好?
struct B {
virtual ~B() throw() { }
};
struct D : B {
virtual ~D() { }
};
Run Code Online (Sandbox Code Playgroud)
(2)此规则如何应用于隐式声明的析构函数?也就是说,以下是否良好?
struct B {
virtual ~B() throw() { }
};
struct D : B {
// ~D() implicitly declared …Run Code Online (Sandbox Code Playgroud) c++ destructor exception-handling exception exception-specification