简体   繁体   English

C ++ 11标准容器是“最终的”吗?

[英]Are C++11 standard containers “final”?

We (should) know that C++ standard library containers, including std::string , are not meant to be inherited from. 我们(应该)知道C ++标准库容器,包括std::string ,并不意味着继承自。 But still, C++98/03 did allow us to do it even if it was leading to bugs. 但是,C ++ 98/03确实允许我们这样做,即使它导致了错误。

Now that the final keyword is available, are those standard library container marked final to prevent bad use of inheritance with them? 既然final关键字可用,那些标准库容器是否标记为final以防止对它们的继承使用不当?

If not, why is that? 如果没有,为什么?

The LWG discussed this issue at the recent meeting in Kona Feb. 6-10, 2012. This is LWG issue 2113 . LWG在最近于2012年2月6日至10日举行的Kona会议上讨论了这个问题。这是LWG 第2113期

The LWG decided to mark LWG 2113 as NAD (not a defect), with the rationale that the standard is already clear that existing classes such as containers and std::string can not be marked final by the implementation. LWG决定将LWG 2113标记为NAD(不是缺陷),理由是该标准已经清楚,现有的类如容器和std::string不能被实现标记为最终。

The discussion included the fact that while it may be frowned on to derive from such classes, it is clearly legal to do so in C++98/03. 讨论包括这样一个事实:尽管从这些类派生出来可能不赞成,但在C ++ 98/03中这样做显然是合法的。 And making it illegal in C++11 would break far too much code. 并且在C ++ 11中将其视为非法会破坏太多代码。

Update 更新

At this time, no library types in the current working draft are marked final . 目前, 当前工作草案中没有库类型标记为final

std::string does not seem to be marked final, nor do the other containers. std::string似乎没有标记为final,其他容器也没有。

My guess as to why would be that even though deriving from them isn't generally recommended, nobody was quite sure how much working code would break if it was prohibited. 我猜测为什么会这样,即使从它们推导出来也不是一般的推荐,没有人确定如果它被禁止会破坏多少工作代码。

Also note that, for what it's worth, final isn't technically a key word -- it's an identifier to which a special meaning is attached, but only under specific circumstances. 还要注意,对于它的价值而言, final在技​​术上并不是一个关键词 - 它是附加了特殊含义的标识符,但仅限于特定情况。 Code that contained something like int final; final = 1; 包含类似int final; final = 1;代码int final; final = 1; int final; final = 1; will still work. 仍然有效。 This is mostly for backward compatibility though -- at least in new code, it's almost certainly better to use final only for the special meaning, not as a normal identifier. 这主要是为了向后兼容 - 至少在新代码中,使用final仅用于特殊含义,而不是普通标识符几乎肯定更好。

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

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