The order of override and noexcept in the standard

风格不统一 提交于 2019-12-22 05:04:19

问题


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.


回答1:


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.




回答2:


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.




回答3:


excerpts from [gram.decl]/A.7, emphasis mine

declarator:
   ptr-declarator
   noptr-declarator parameters-and-qualifiers trailing-return-type

ptr-declarator:
   noptr-declarator
   ptr-operator ptr-declarator

noptr-declarator:
   declarator-id attribute-specifier-seqopt
   noptr-declarator parameters-and-qualifiers
   noptr-declarator [ constant-expressionopt ] attribute-specifier-seqopt
   ( ptr-declarator )

parameters-and-qualifiers:
( parameter-declaration-clause ) attribute-specifier-seqopt cv-qualifier-seqopt
ref-qualifieroptexception-specificationopt

[gram.class]/A.8, emphasis mine

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

That is, the exception-specification is part of the declarator, which comes before the virt-specifier in a member-declarator.



来源:https://stackoverflow.com/questions/21577466/the-order-of-override-and-noexcept-in-the-standard

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!