<QtSwap> 代理页面
函数
void | qSwap(T &lhs, T &rhs) |
函数文档
[constexpr noexcept(...)]
template <typename T> void qSwap(T &lhs, T &rhs)
交换变量lhs 和rhs 的值,同时考虑特定类型的swap()
重载。
该函数是 Qt 版本的 boost::swap()
,相当于
using std::swap; // bring std::swap into scope (for built-in types) swap(lhs, rhs); // unqualified call (picks up type-specific overloads // via Argument-Dependent Lookup, or falls back to std::swap)
主要在通用代码中使用该函数,因为您对T
一无所知,所以传统上会写上面两行代码。
如果您已经知道T
是什么,则按优先顺序使用以下选项之一:
lhs.swap(rhs);
如果存在这种成员交换std::swap(lhs, rhs);
如果不存在特定类型的swap()
详情请参见boost.org 上的boost::swap()
。
另请参见cppreference.com 上的std::swap
,cppreference. com 上的Swappable
。
注: 当std::is_nothrow_swappable_v<T>
为true
时,此函数为 noexcept。
© 2025 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.