noexceptnoexcept operator performs a compile-time check that returns true if an expression is declared to not throw any exceptions.bool.noexcept conditions.noexcept condition is part of the specified interface. Overwriting a base class function that is noexcept in a derived class with a function that is not noexcept is an error (but not the other way around).
class Ancestor {
public:
virtual void foo() noexcept {};
virtual void foobar() noexcept {};
virtual void bar() {};
void baz() noexcept {};
};
class Descendant : public Ancestor {
public:
// OK
void foo() noexcept override{}
// ERROR, overriding cannot discard exception-specification
//void foobar() override {};
// OK, overriding can add except-specification
void bar() noexcept override{}
// OK, hiding instead of overriding
void baz() {}
};
int main() { }
class Ancestor {
virtual void foo1() noexcept(true) {};
virtual void foo2() noexcept(true) {};
virtual void foo3() noexcept(false) {};
virtual void foo4() noexcept(false) {};
};
class Descendant : public Ancestor {
void foo1() noexcept(true) override {};
//void foo2() noexcept(false) override {}; // Error!
void foo3() noexcept(true) override {};
void foo4() noexcept(false) override {};
};
int main() { }