为什么C ++容器不允许不完全类型? [英] Why C++ containers don't allow incomplete types?

查看:175
本文介绍了为什么C ++容器不允许不完全类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么C ++不允许不完整类型的容器被实例化?

Why doesn't C++ allow containers of incomplete types to be instantiated?

编写没有此限制的容器( boost :: container) 完全有能力做到这一点。据我所见,它似乎没有给出任何性能或其他类型的增益,但标准声明它是未定义的行为。

It's certainly possible to write containers that don't have this restriction -- boost::container is completely capable of doing this. As far as I can see, it doesn't seem to give any performance or other type of gain, and yet the standard declares it to be undefined behavior.

会阻止建立递归数据结构。

It does prevent recursive data structures from being built, for example.

那么,C ++标准强加了这个任意的限制呢?

What, then does the C++ standard impose this arbitrary restriction? What would have been the downside of allowing incomplete types as template parameters wherever possible?

推荐答案

Matt Austern是C ++标准化的主席,委员会的图书馆工作组在委员会的 Dr. Dobb的文章由于历史原因:

Matt Austern, the chair of the C++ standardization committee's library working group, explained this decision of the committee in his Dr. Dobb's article by historical reasons:


我们发现,更多的测试,即使[simple]与每个STL实现。最后,这一切看起来太模糊,太不明白了;标准化委员会认为没有任何选择,只是说,STL容器不应该与不完全类型工作。

We discovered, with more testing, that even the [simple] example didn't work with every STL implementation. In the end, it all seemed too murky and too poorly understood; the standardization committee didn't think there was any choice except to say that STL containers aren't supposed to work with incomplete types. For good measure, we applied that prohibition to the rest of the standard library too.

我的理解是,委员会没有

My understanding of this is that the committee did not want to invalidate existing implementations of the library by requiring them to support incomplete types retroactively.

在同一篇文章中,他承认

In the same article he concedes that


在未来的C ++版本中,放宽对不完全类型实例化标准库模板的限制可能是有意义的。

In a future revision of C++, it might make sense to relax the restriction on instantiating standard library templates with incomplete types.

鉴于本文可以追溯到2002年,并且禁令在当前标准中仍然存在,我认为提升设计者不要等待未来,并建立自己的容器的决定允许不完全类型是完全合理的。

Given that the article dates back to 2002, and the prohibition remains in place in the current standard, I think that the decision of the boost designers not to wait for the future and build their own containers that allow incomplete types was fully justified.

这篇关于为什么C ++容器不允许不完全类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆