1// Copyright (C) 2022 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only
5 \fn template <typename Enum> std::underlying_type_t<Enum> qToUnderlying(Enum e)
6 \relates <QtTypeTraits>
9 Converts the enumerator \a e to the equivalent value expressed in its
10 enumeration's underlying type.
14 \fn template <typename T> typename std::add_const<T>::type &qAsConst(T &t)
15 \relates <QtTypeTraits>
18 \deprecated [6.6] Use std::as_const() instead.
20 Returns \a t cast to \c{const T}.
22 This function is a Qt implementation of C++17's std::as_const(),
23 a cast function like std::move(). But while std::move() turns
24 lvalues into rvalues, this function turns non-const lvalues into
25 const lvalues. Like std::as_const(), it doesn't work on rvalues,
26 because it cannot be efficiently implemented for rvalues without
27 leaving dangling references.
29 Its main use in Qt is to prevent implicitly-shared Qt containers
31 \snippet code/src_corelib_global_qglobal.cpp as-const-0
33 Of course, in this case, you could (and probably should) have declared
34 \c s as \c const in the first place:
35 \snippet code/src_corelib_global_qglobal.cpp as-const-1
36 but often that is not easily possible.
38 It is important to note that qAsConst() does not copy its argument,
39 it just performs a \c{const_cast<const T&>(t)}. This is also the reason
40 why it is designed to fail for rvalues: The returned reference would go
41 stale too soon. So while this works (but detaches the returned object):
42 \snippet code/src_corelib_global_qglobal.cpp as-const-2
45 \snippet code/src_corelib_global_qglobal.cpp as-const-3
47 To prevent this construct from compiling (and failing at runtime), qAsConst() has
48 a second, deleted, overload which binds to rvalues.
52 \fn template <typename T> void qAsConst(const T &&t)
53 \relates <QtTypeTraits>
59 This overload is deleted to prevent a dangling reference in code like
60 \snippet code/src_corelib_global_qglobal.cpp as-const-4
64 \fn template <typename T, typename U = T> T qExchange(T &obj, U &&newValue)
65 \relates <QtTypeTraits>
68 Replaces the value of \a obj with \a newValue and returns the old value of \a obj.
70 This is Qt's implementation of std::exchange(). It differs from std::exchange()
71 only in that it is \c constexpr already before C++20 and noexcept already before C++23.
73 We strongly advise to use std::exchange() when you don't need the C++20 or C++23 variants.
75 Here is how to use qExchange() to implement move constructors:
77 MyClass(MyClass &&other)
78 : m_pointer{qExchange(other.m_pointer, nullptr)},
79 m_int{qExchange(other.m_int, 0)},
80 m_vector{std::move(other.m_vector)},
84 For members of class type, we can use std::move(), as their move-constructor will
85 do the right thing. But for scalar types such as raw pointers or integer type, move
86 is the same as copy, which, particularly for pointers, is not what we expect. So, we
87 cannot use std::move() for such types, but we can use std::exchange()/qExchange() to
88 make sure the source object's member is already reset by the time we get to the
89 initialization of our next data member, which might come in handy if the constructor
90 exits with an exception.
92 Here is how to use qExchange() to write a loop that consumes the collection it
95 for (auto &e : qExchange(collection, {})
99 Which is equivalent to the following, much more verbose code:
102 auto tmp = std::move(collection);
103 collection = {}; // or collection.clear()
109 This is perfectly safe, as the for-loop keeps the result of qExchange() alive for as
110 long as the loop runs, saving the declaration of a temporary variable. Be aware, though,
111 that qExchange() returns a non-const object, so Qt containers may detach.