为什么Qt的容器类不允许可移动的,不可复制的元素类型? [英] Why do Qt's container classes not allow movable, non-copyable element types?

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

问题描述

Qt容器类QList<T>QVector<T>等要求其元素类型是可复制的.从C ++ 11开始,STL容器要求其元素类型是可复制的或仅可移动的.为什么Qt容器不支持仅移动元素类型?

The Qt container classes QList<T>, QVector<T> etc. require their element types to be copyable. Since C++11, the STL containers require their element type to be copyable or movable only. Why do the Qt containers not support move-only element types?

推荐答案

Qt错误#54685 已从Qt开发人员那里得到明确的确认,因为Qt容器的隐式共享原则

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.

将一个Qt容器复制到另一个Qt容器时,并不是在进行深层复制-容器内部共享它们的内容.只有在容器上调用修改函数时,它才会分离,从而创建其自己的内容本地副本.这样一来,Qt容器就可以通过信号和插槽(必须按值)传递,而不会降低性能.

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.

当包含的类型为仅移动类型时,这当然是不可能的.而且,按值传递容器的能力(不复制其内容)对于Qt的元对象机制至关重要,因此我认为无法对其进行重新设计. Qt API依赖于隐式共享并按值传递容器,即使仅移动容器将通过引用传递,因此也没有简单的出路.

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.

这篇关于为什么Qt的容器类不允许可移动的,不可复制的元素类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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