Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qswap.qdoc
Go to the documentation of this file.
1// Copyright (C) 2022 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only
3
4/*!
5 \headerfile <QtSwap>
6 \inmodule QtCore
7 \ingroup funclists
8 \brief Swapping the values of two variables.
9*/
10
11/*!
12 \fn template <typename T> void qSwap(T &lhs, T &rhs)
13 \relates <QtSwap>
14
15 Exchanges the values of variables \a lhs and \a rhs,
16 taking type-specific \c{swap()} overloads into account.
17
18 This function is Qt's version of
19 \l{https://www.boost.org/doc/libs/release/libs/core/doc/html/core/swap.html}{\c{boost::swap()}},
20 and is equivalent to
21 \code
22 using std::swap; // bring std::swap into scope (for built-in types)
23 swap(lhs, rhs); // unqualified call (picks up type-specific overloads
24 // via Argument-Dependent Lookup, or falls back to std::swap)
25 \endcode
26
27 Use this function primarily in generic code, where you would traditionally
28 have written the above two lines, because you don't know anything about \c{T}.
29
30 If you already know what \c{T} is, then use one of the following options, in
31 order of preference:
32
33 \list
34 \li \c{lhs.swap(rhs);} if such a member-swap exists
35 \li \c{std::swap(lhs, rhs);} if no type-specific \c{swap()} exists
36 \endlist
37
38 See
39 \l{https://www.boost.org/doc/libs/release/libs/core/doc/html/core/swap.html}{\c{boost::swap()} on boost.org}
40 for more details.
41
42 See also
43 \l{https://en.cppreference.com/w/cpp/algorithm/swap}{\c{std::swap} on cppreference.com},
44 \l{https://en.cppreference.com/w/cpp/named_req/Swappable}{\c{Swappable} on cppreference.com}.
45*/