Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The order of override and noexcept in the standard

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.

like image 520
FrozenHeart Avatar asked Feb 05 '14 12:02

FrozenHeart


People also ask

What does Noexcept mean?

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.

What does Noexcept mean in C ++?

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.

What happens when Noexcept function throws?

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.


2 Answers

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.

like image 89
Simple Avatar answered Oct 02 '22 15:10

Simple


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.

like image 28
PlasmaHH Avatar answered Oct 02 '22 16:10

PlasmaHH