为什么 "override" 在 C++11 的末尾?
Why "override" is at the end in C++11?
我想知道为什么在 C++11 中,他们必须在方法的末尾添加 override
关键字,而不是像 virtual
那样在开头添加关键字。我没有看到能够在方法声明中同时编写 virtual
和 override
的兴趣。
是否有技术原因导致委员会在需要时没有选择简单地编写 override
而不是 virtual
?
谢谢!
关于添加关键字控制覆盖的提议 (override
/final
) , paper N3151 ,让我们对这个选择有了一些了解(强调我的):
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.
注:
C++ 11 标准在第 § 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
这肯定是有技术原因的!您可以在 this article.
中阅读所有相关信息
简而言之,override
是一个上下文相关的关键字,这意味着您也可以将其用作标识符。这样做是为了避免破坏使用此标识符的现有代码。这意味着它必须出现在不允许使用标识符的位置,即紧跟在函数声明的右括号之后。
我想知道为什么在 C++11 中,他们必须在方法的末尾添加 override
关键字,而不是像 virtual
那样在开头添加关键字。我没有看到能够在方法声明中同时编写 virtual
和 override
的兴趣。
是否有技术原因导致委员会在需要时没有选择简单地编写 override
而不是 virtual
?
谢谢!
关于添加关键字控制覆盖的提议 (override
/final
) , paper N3151 ,让我们对这个选择有了一些了解(强调我的):
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.
注:
C++ 11 标准在第 § 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
这肯定是有技术原因的!您可以在 this article.
中阅读所有相关信息简而言之,override
是一个上下文相关的关键字,这意味着您也可以将其用作标识符。这样做是为了避免破坏使用此标识符的现有代码。这意味着它必须出现在不允许使用标识符的位置,即紧跟在函数声明的右括号之后。