<QtSwap>
交换两个变量的值。更多...
| Header: | #include <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。
© 2026 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.