Is the order of override and noexcept required by the standard?
class Base
{
public:
virtual void foo() {}
};
class Derived : public Base
{
public:
// virtual void foo() override {} // Ok
// virtual void foo() noexcept {} // Ok
// virtual void foo() noexcept override {} // Ok
virtual void foo() override noexcept {} // Error
};
int main() {}
I'm using gcc 4.7.2.
The noexcept operator performs a compile-time check that returns true if an expression is declared to not throw any exceptions. It can be used within a function template's noexcept specifier to declare that the function will throw exceptions for some types but not others.
A noexcept-expression is a kind of exception specification: a suffix to a function declaration that represents a set of types that might be matched by an exception handler for any exception that exits a function.
Note that noexcept doesn't actually prevent the function from throwing exceptions or calling other functions that are potentially throwing. Rather, when an exception is thrown, if an exception exits a noexcept function, std::terminate will be called.
In 8.3.5 [dcl.fct] we see:
D1 ( parameter-declaration-clause ) cv-qualifier-seq opt ref-qualifier opt exception-specification opt attribute-specifier-seq opt
...and in 9.2 [class.mem] we see:
declarator virt-specifier-seq opt pure-specifier opt
This states that override
and final
have to come after noexcept
.
Actually, yes, it is, its just hard to find out, since its a bit scattered. Annex A (Grammar summary) is of some help here. Lets try to find the specific bits:
declarator:
ptr-declarator
noptr-declarator parameters-and-qualifiers trailing-return-type
parameters-and-qualifiers:
( parameter-declaration-clause ) attribute-specifier-seqopt cv-qualifier-seqopt
ref-qualifieropt exception-specificationot
exception-specification:
dynamic-exception-specification
noexcept-specification
noexcept-specification:
noexcept ( constant-expression )
noexcept
and then later for override
member-declarator:
declarator virt-specifier-seqopt pure-specifieropt
declarator brace-or-equal-initializeropt
identifieropt attribute-specifier-seqopt: constant-expression
virt-specifier-seq:
virt-specifier
virt-specifier-seq virt-specifier
virt-specifier:
override
final
So a declarator is the thing that contains the noexcept keyword, but in the member-declarator the virt-specifier comes after the declarator.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With