“override”限定符在哪里与尾返回类型? [英] Where does the 'override' qualifier go with trailing return types?

查看:750
本文介绍了“override”限定符在哪里与尾返回类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C ++ 11具有新的 override 限定符,可以应用于成员函数,以声明它们覆盖基类中的虚函数。 C ++ 11也允许尾随返回类型,所以函数可以声明为 auto f() - > return_type 。当我结合这两个特性,我不知道 override 是在 - > 之前还是之后。 / p>

例如,假设我们有以下基类:

  struct Base {
virtual auto f()const - > int = 0;
};

派生类的两种可能性是:

  struct派生:public Base {
virtual auto f()const override - > int {return 0; } //编译g ++ 4.7.1
};

  struct Derived:public Base {
virtual auto f()const - > int override {return 0; } //编译clang ++ 4.0
};

g ++ 4.7.1编译第一个版本,但第二个版本失败,

$ b $成员声明结束时,b

  test.cpp:6:30:error:expected';'
test.cpp:6:34:error: 'override'没有命名类型

,而clang ++ 4.0编译第二个,但第一个失败

  test.cpp:6:11:error:'auto'return without trailing return type 
virtual auto f )const override - > int {return 0; }
^
test.cpp:6:3:错误:只有virtual成员函数可以标记为'override'
virtual auto f()const override - > int {return 0; }
^ ~~~~~~~~
test.cpp:6:35:error:expected';'在声明列表的结尾
virtual auto f()const override - > ; int {return 0; }

这些编译器中哪些正在根据标准做正确的事?



修改:Kerrek SB注意到,这是

根据标准8.4.1,函数的声明符包括 trailing-return-type ,类函数定义包含 declarator virt-specifier-seq opt 。第二个 virt-specifier-seq final override 因此这些字符在尾随返回类型之后。 (即Clang得到正确的结果。)


C++11 has the new override qualifier which can be applied to member functions to assert that they override a virtual function in the base class. C++11 also allows trailing return types so functions can be declared as auto f() -> return_type. When I combine both these features, I don't know whether override goes before or after the ->.

For example, suppose we have the following base class:

struct Base {
    virtual auto f () const -> int = 0;
};

The two possibilities for a derived class are:

struct Derived : public Base {
    virtual auto f () const override -> int { return 0; } // Compiles on g++ 4.7.1
};

or

struct Derived : public Base {
    virtual auto f () const -> int override { return 0; } // Compiles on clang++ 4.0
};

g++ 4.7.1 compiles the first version but fails on the second with

test.cpp:6:30: error: expected ';' at end of member declaration
test.cpp:6:34: error: 'override' does not name a type

whereas clang++ 4.0 compiles the second one but fails on the first with

test.cpp:6:11: error: 'auto' return without trailing return type
  virtual auto f () const override -> int { return 0; }
          ^
test.cpp:6:3: error: only virtual member functions can be marked 'override'
  virtual auto f () const override -> int { return 0; }
  ^                       ~~~~~~~~
test.cpp:6:35: error: expected ';' at end of declaration list
  virtual auto f () const override -> int { return 0; }

Which of these compilers is actually doing the right thing according to the standard?

Edit: As Kerrek SB notes, this is a bug in gcc (Bugzilla link).

解决方案

According to the standard, 8.4.1, a declarator for a function includes the trailing-return-type, and a class function definition contains "declarator virt-specifier-seqopt". The second one, virt-specifier-seq, is one of final or override, so those come after the trailing return type. (I.e. Clang gets it right.)

这篇关于“override”限定符在哪里与尾返回类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆