简体   繁体   English

为什么C ++ 11会覆盖而最终不是属性?

[英]Why are C++11 override and final not attributes?

I somehow missed that attributes were introduced in C++11. 我不知道错过了C ++ 11中引入的属性。 Now I found out, I'm wondering why override and final were added as identifiers with special meaning and not as standard attributes. 现在我发现了,我想知道为什么overridefinal被添加为具有特殊含义的标识符而不是标准属性。

The purpose of override is to generate a compile time error and this is also the purpose of many of the standard attributes. override的目的是生成编译时错误,这也是许多标准属性的目的。 It feels as if they would fit in that concept but there is probably a reason for it that I'm missing. 感觉好像他们会适应这个概念,但我可能有理由错过这个概念。

They were, once, before they got changed in response to comment US 44 on C++11's FCD: 在他们改变之前,他们曾经回应过关于C ++ 11 FCD的评论US 44

Even if attributes continue to be standardized over continued objections from both of the two vendors who are cited as the principal prior art, we can live with them with the exception of the virtual override controls. 即使属性继续被标准化而不是被引用作为主要现有技术的两个供应商的持续反对,我们也可以使用它们,但虚拟覆盖控制除外。 This result is just awful, as already shown in the example in 7.6.5 (excerpted): 这个结果很糟糕,如7.6.5中的示例所示(摘录):

 class D [[base_check]] : public B { void some_func [[override]] (); virtual void h [[hiding]] (char*); }; 

Here we have six keywords (not counting void and char ): three normal keywords, and three [[decorated]] keywords. 这里我们有六个关键字(不计算voidchar ):三个普通关键字和三个[[decorated]]关键字。 There has already been public ridicule of C++0x about this ugliness. 关于这种丑陋,已经公开嘲笑C ++ 0x。 This is just a poor language design, even in the face of backward compatibility concerns (eg, that some existing code may already use those words as identifiers) because those concerns have already been resolved in other ways in existing practice (see below). 这只是一种糟糕的语言设计,即使面对向后兼容性问题(例如,某些现有代码可能已经将这些单词用作标识符),因为这些问题已经在现有实践中以其他方式解决(见下文)。 More importantly, this is exactly the abuse of attributes as disguised keywords that was objected to and was explicitly promised not to happen in order to get this proposal passed. 更重要的是,这正是滥用属性作为被反对的伪装关键字,并明确承诺不会发生以便通过此提案。 The use of attributes for the virtual control keywords is the most egregious abuse of the attribute syntax, and at least that use of attributes must be fixed by replacing them with non-attribute syntax. 虚拟控件关键字的属性的使用是对属性语法的最严重滥用,并且至少必须通过用非属性语法替换它们来修复属性的使用。 These virtual override controls are language features, not annotations. 这些虚拟覆盖控件是语言功能,而不是注释。

It is possible to have nice names and no conflicts with existing code by using contextual keywords, such as recognizing the word as having the special meaning when it appears in a grammar position where no user identifier can appear, as demonstrated in C++/CLI which has five years of actual field experience with a large number of customers (and exactly no name conflict or programmer confusion problems reported in the field during the five years this has been available): 通过使用上下文关键字,可以使用好的名称并且不会与现有代码发生冲突,例如,当它出现在没有用户标识符的语法位置时,将该单词识别为具有特殊含义,如C ++ / CLI中所示。在大量客户的五年实际现场经验(并且在五年内该领域报告的名称冲突或程序员混淆问题完全没有):

 class D : public B { void some_func() override; // same meaning as [[override]] - explicit override virtual void h (char*) new; // same meaning as [[hiding]] - a new function, not an override }; int override = 42; // ok, override is not a reserved keyword 

The above forms are implementable, have been implemented, have years of practical field experience, and work. 以上表格可实施,已实施,具有多年的实践经验和工作。 Developers love them. 开发人员喜欢它们。 Whether the answer is to follow this existing practice or something else, there needs to be a more natural replacement for the currently [[attributed]] keywords for virtual override control which is an ugly novelty that has no field experience and that developers have already ridiculed. 无论答案是遵循现有的做法还是别的,对于虚拟覆盖控制的当前[[attributed]]关键字需要更自然的替代,这是一个没有现场经验且开发人员已经嘲笑的丑陋新奇事物。

To simply quote http://en.cppreference.com/w/cpp/language/attributes : 简单引用http://en.cppreference.com/w/cpp/language/attributes

Attributes provide the unified standard syntax for implementation-defined language extensions, such as the GNU and IBM language extensions attribute ((...)), Microsoft extension __declspec(), etc. 属性为实现定义的语言扩展提供统一的标准语法,例如GNU和IBM语言扩展属性 ((...)),Microsoft扩展__declspec()等。

This makes it quite clear that standard language keywords are not attributes. 这清楚地表明标准语言关键字不是属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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