Qt 6.x
The Qt SDK
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
qttypetraits.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 \fn template <typename Enum> std::underlying_type_t<Enum> qToUnderlying(Enum e)
6 \relates <QtTypeTraits>
7 \since 6.2
8
9 Converts the enumerator \a e to the equivalent value expressed in its
10 enumeration's underlying type.
11*/
12
13/*!
14 \fn template <typename T> typename std::add_const<T>::type &qAsConst(T &t)
15 \relates <QtTypeTraits>
16 \since 5.7
17
18 \deprecated [6.6] Use std::as_const() instead.
19
20 Returns \a t cast to \c{const T}.
21
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.
28
29 Its main use in Qt is to prevent implicitly-shared Qt containers
30 from detaching:
31 \snippet code/src_corelib_global_qglobal.cpp as-const-0
32
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.
37
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
43
44 this would not:
45 \snippet code/src_corelib_global_qglobal.cpp as-const-3
46
47 To prevent this construct from compiling (and failing at runtime), qAsConst() has
48 a second, deleted, overload which binds to rvalues.
49*/
50
51/*!
52 \fn template <typename T> void qAsConst(const T &&t)
53 \relates <QtTypeTraits>
54 \since 5.7
55 \overload
56
57 \deprecated [6.6]
58
59 This overload is deleted to prevent a dangling reference in code like
60 \snippet code/src_corelib_global_qglobal.cpp as-const-4
61*/
62
63/*!
64 \fn template <typename T, typename U = T> T qExchange(T &obj, U &&newValue)
65 \relates <QtTypeTraits>
66 \since 5.14
67
68 Replaces the value of \a obj with \a newValue and returns the old value of \a obj.
69
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.
72
73 We strongly advise to use std::exchange() when you don't need the C++20 or C++23 variants.
74
75 Here is how to use qExchange() to implement move constructors:
76 \code
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)},
81 ...
82 \endcode
83
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.
91
92 Here is how to use qExchange() to write a loop that consumes the collection it
93 iterates over:
94 \code
95 for (auto &e : qExchange(collection, {})
96 doSomethingWith(e);
97 \endcode
98
99 Which is equivalent to the following, much more verbose code:
100 \code
101 {
102 auto tmp = std::move(collection);
103 collection = {}; // or collection.clear()
104 for (auto &e : tmp)
105 doSomethingWith(e);
106 } // destroys 'tmp'
107 \endcode
108
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.
112*/