简体   繁体   中英

Why “override” is at the end in C++11?

I'm trying to see the reason why, in C++11, they had to add the override keyword at the end of the method instead of the beginning like virtual . I don't see the interest of being able to write both virtual and override in the declaration of a method.

Is there a technical reason why the committee didn't choose to simply be able to write override instead of virtual when it was needed?

Thanks!

The proposal for the addition of the keywords controlling override ( override / final ) , paper N3151 , gives us some insight about this choice (emphasis mine) :

It is preferable to put such virtual control keywords at the end of the declaration so that they don't clash with eg. return types at the beginning of declarations.

[...]

For context-insensitive, normal keywords, it's less important where the keywords are placed because the words are reserved. We could put them at the beginning of declarations or at the end.

During the discussion of attributes, Francis Glassborow pointed out that the beginning of declarations is becoming crowded. If we put the virtual control keywords at the beginning, we can end up with examples like the one below:

struct B
{
   virtual volatile const unsigned long int f()
      volatile const noexcept;
   void f(int g);
};

struct D : B
{
   virtual hides_name virtual_override final_overrider volatile const unsigned long int f()
      volatile const noexcept;
};

Putting the new keywords at the end at least alleviates the situation somewhat:

struct B
{
   virtual volatile const unsigned long int f()
      volatile const noexcept;
   void f(int g);
};

struct D : B
{
   virtual volatile const unsigned long int f()
      hides_name virtual_override final_overrider volatile const noexcept;
};

There are people who think these control keywords should be in the same place with virtual. As mentioned, that place is already crowded.


Note:

The C++ 11 Standard defines context sensitive keywords in section § 2.11 / 2 [lex.name] :

The identifiers in Table 3 have a special meaning when appearing in a certain context. When referred to in the grammar, these identifiers are used explicitly rather than using the identifier grammar production. Unless otherwise specified, any ambiguity as to whether a given identifier has a special meaning is resolved to interpret the token as a regular identifier.

Table3:

final override

There certainly is a technical reason for it! You can read all about it in this article .

Briefly, override is a context-sensitive keyword, which means that you can also use it as an identifier. It was done this way to avoid breaking existing code that uses this identifier. That means that it has to appear in a position where identifiers are not allowed, namely immediately after the closing parenthesis of a function declaration.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM