Rule A15-4-3 (required, implementation, automated)

The noexcept specification of a function shall either be identical across all translation units, or identical or more restrictive between a virtual member function and an overrider.

Rationale

Declarations of the same function, even in different translation units, have to specify the same noexcept specification. Overriding functions have to specify the same or a stricter noexcept specification than the base class function which they override.

Note that in many cases, a violation of this rule will lead to a compilation error. This is not guaranteed, however, in particular when function declarations appear in separate translation units.

Example

//% $Id: A15-4-3.cpp 317753 2018-04-27 07:44:02Z jan.babst $
// f1.hpp
void Fn() noexcept;

// f1.cpp
// #include <f1.hpp>
void Fn() noexcept // Compliant
{
// Implementation
}

// f2.cpp
// #include <f1.hpp>
void Fn() noexcept(false) // Non-compliant - different exception specifier
{
// Implementation
}

class A
{
public:
void F() noexcept;
void G() noexcept(false);
virtual void V1() noexcept = 0;
virtual void V2() noexcept(false) = 0;
};
void A::F() noexcept // Compliant
// void A::F() noexcept(false) // Non-compliant - different exception specifier
// than in declaration
{
// Implementation
}
void A::G() noexcept(false) // Compliant
// void A::G() noexcept // Non-compliant - different exception specifier than
// in declaration
{
// Implementation
}
class B : public A
{
public:
void V1() noexcept override // Compliant
// void V1() noexcept(false) override // Non-compliant - less restrictive
// exception specifier in derived method, non-compilable
{
// Implementation

}
void V2() noexcept override // Compliant - stricter noexcept specification
{
// Implementation
}
};

See also

MISRA C++ 2008 [7]: 15-4-1: If a function is declared with an exceptionspecification, then all declarations of the same function (in other translation units) shall be declared with the same set of type-ids.