<QtSwap>

交换两个变量的值。更多

Header: #include <QtSwap>

函数

void qSwap(T &lhs, T &rhs)

详细说明

函数文档

[constexpr noexcept(...)] template <typename T> void qSwap(T &lhs, T &rhs)

交换变量lhsrhs 的值,同时考虑特定类型的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.