gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:26:39 25 4
gpt4 key购买 nike

我不知何故错过了 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com