Why do Qt's container classes not allow movable, non-copyable element types?

◇◆丶佛笑我妖孽 提交于 2019-11-28 13:18:48

Qt bug #54685 has explicit confirmation from Qt developers that move-only types are not (and will never be) supported because of Qt containers' principle of implicit sharing.

When you copy one Qt container to another, you're not doing a deep copy—the containers share their contents internally. Only when a modifying function is called on a container does it detach, creating its own local copy of the contents. This allows Qt containers to be passed around through signals and slots (which is necessarily by value) without the performance plummetting.

This would of course be impossible when the contained type is move-only. And the ability to pass containers around by value (without copying their contents) is fundamental to Qt's meta-object mechanism, so I do not think it could be redesigned. Qt APIs rely on implicit sharing and pass containers around by value even where a move-only container would be passed by reference, so there is no easy way out.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!