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?
相关问题
- QML: Cannot read property 'xxx' of undefin
- thread_local variables initialization
- QTextEdit.find() doesn't work in Python
- Google Test - generate values for template class i
- std::vector of objects / pointers / smart pointers
相关文章
- ubuntu20.4中c#通过c++库调用python脚本
- Qt槽函数自动执行多遍
- Is there a non-java, cross platform way to launch
- How to get a settings storage path in a cross-plat
- std::function copying parameters?
- Why doesn't valgrind detect a memory leak in m
- Is a compiler forced to reject invalid constexpr?
- QTreeView remove decoration/expand button for all
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.