c++ - 为什么 C++11 override 和 final 不是属性?

标签 c++ c++11

我不知何故错过了 C++11 中引入的属性。现在我发现了,我想知道为什么 overridefinal 被添加为具有特殊含义的标识符,而不是作为标准属性。

override 的目的是产生编译时错误,这也是许多标准属性的目的。感觉好像它们符合这个概念,但我可能没有找到它的原因。

最佳答案

他们曾经是,在他们因评论而改变之前 US 44在 C++11 的 FCD 上:

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):

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. There has already been public ridicule of C++0x about this ugliness. This is just a poor language design, even in the face of backward compatibility concerns (e.g., 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):

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.

关于c++ - 为什么 C++11 override 和 final 不是属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41637559/

相关文章:

c++ - 具有 extern #define 和 typedef 结构的静态库

c++ - 将 alpha channel 添加到 opencv Mat

c++ - 如何在 C++ 中正确使用嵌套类?

c++ - C++ 中的内存模型和单例

c++ - 在结构 C++ 中使用 std::swap 和 std::vector

c++ - 为什么 gcc-4.9.2 不支持 std::string.insert(iterator, range) 返回迭代器

c++ - 闭包捕获的变量存储在哪里?

C++11 move 语义

c++ - 如何使用枚举将 char 值映射到 int

c++ - 将 boost lambda 与复合表达式一起使用