1// Copyright (C) 2022 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#ifndef QTTYPETRAITS_H
5#define QTTYPETRAITS_H
6
7#include <QtCore/qtconfigmacros.h>
8#include <QtCore/qtdeprecationmarkers.h>
9
10#include <type_traits>
11#include <utility>
12
13#if 0
14#pragma qt_class(QtTypeTraits)
15#pragma qt_sync_stop_processing
16#endif
17
18QT_BEGIN_NAMESPACE
19
20// like std::to_underlying
21template <typename Enum>
22constexpr std::underlying_type_t<Enum> qToUnderlying(Enum e) noexcept
23{
24 return static_cast<std::underlying_type_t<Enum>>(e);
25}
26
27#ifndef QT_NO_AS_CONST
28#if QT_DEPRECATED_SINCE(6, 6)
29
30// this adds const to non-const objects (like std::as_const)
31template <typename T>
32QT_DEPRECATED_VERSION_X_6_6("Use std::as_const() instead.")
33constexpr typename std::add_const<T>::type &qAsConst(T &t) noexcept { return t; }
34// prevent rvalue arguments:
35template <typename T>
36void qAsConst(const T &&) = delete;
37
38#endif // QT_DEPRECATED_SINCE(6, 6)
39#endif // QT_NO_AS_CONST
40
41#ifndef QT_NO_QEXCHANGE
42
43// like std::exchange
44template <typename T, typename U = T>
45constexpr T qExchange(T &t, U &&newValue)
46noexcept(std::conjunction_v<std::is_nothrow_move_constructible<T>,
47 std::is_nothrow_assignable<T &, U>>)
48{
49 T old = std::move(t);
50 t = std::forward<U>(newValue);
51 return old;
52}
53
54#endif // QT_NO_QEXCHANGE
55
56namespace QtPrivate {
57// helper to be used to trigger a "dependent static_assert(false)"
58// (for instance, in a final `else` branch of a `if constexpr`.)
59template <typename T> struct type_dependent_false : std::false_type {};
60template <auto T> struct value_dependent_false : std::false_type {};
61}
62
63QT_END_NAMESPACE
64
65#endif // QTTYPETRAITS_H
66

source code of qtbase/src/corelib/global/qttypetraits.h