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
qxptype_traits.h
Go to the documentation of this file.
1// Copyright (C) 2023 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Marc Mutz <marc.mutz@kdab.com>, Giuseppe D'Angelo <giuseppe.dangelo@kdab.com>
2// Copyright (C) 2022 The Qt Company Ltd.
3// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
4
5#ifndef QXPTYPE_TRAITS_H
6#define QXPTYPE_TRAITS_H
7
8#include <QtCore/qtconfigmacros.h>
9#include <QtCore/qcompilerdetection.h>
10
11#include <QtCore/q20type_traits.h>
12
13//
14// W A R N I N G
15// -------------
16//
17// This file is not part of the Qt API. Types and functions defined in this
18// file can reliably be replaced by their std counterparts, once available.
19// You may use these definitions in your own code, but be aware that we
20// will remove them once Qt depends on the C++ version that supports
21// them in namespace std. There will be NO deprecation warning, the
22// definitions will JUST go away.
23//
24// If you can't agree to these terms, don't use these definitions!
25//
26// We mean it.
27//
28
29QT_BEGIN_NAMESPACE
30
31// like std::experimental::{nonesuch,is_detected/_v}(LFTSv2)
32namespace qxp {
33
34struct nonesuch {
35 ~nonesuch() = delete;
36 nonesuch(const nonesuch&) = delete;
37 void operator=(const nonesuch&) = delete;
38};
39
40namespace _detail {
41 template <typename T, typename Void, template <typename...> class Op, typename...Args>
42 struct detector {
44 using type = T;
45 };
46 template <typename T, template <typename...> class Op, typename...Args>
47 struct detector<T, std::void_t<Op<Args...>>, Op, Args...> {
49 using type = Op<Args...>;
50 };
51} // namespace _detail
52
53template <template <typename...> class Op, typename...Args>
54using is_detected = typename _detail::detector<qxp::nonesuch, void, Op, Args...>::value_t;
55
56template <template <typename...> class Op, typename...Args>
57constexpr inline bool is_detected_v = qxp::is_detected<Op, Args...>::value;
58
59template <typename Default, template <typename...> class Op, typename...Args>
61
62template <typename Default, template <typename...> class Op, typename...Args>
63using detected_or_t = typename qxp::detected_or<Default, Op, Args...>::type;
64
65template <template <typename...> class Op, typename...Args>
67
68// qxp::is_virtual_base_of_v<B, D> is true if and only if B is a virtual base class of D.
69// Just like is_base_of:
70// * only works on complete types;
71// * B and D must be class types;
72// * ignores cv-qualifications;
73// * B may be inaccessibile.
74
75#ifdef __cpp_lib_is_virtual_base_of
78#else
79namespace _detail {
80 // Check that From* can be converted to To*, ignoring accessibility.
81 // This can be done using a C cast (see [expr.cast]/4).
82QT_WARNING_PUSH
83QT_WARNING_DISABLE_GCC("-Wold-style-cast")
84QT_WARNING_DISABLE_CLANG("-Wold-style-cast")
85 template <typename From, typename To>
87 (To *)std::declval<From *>()
88 );
90
91 template <typename Base, typename Derived, typename = void>
93
94 template <typename Base, typename Derived>
99 // Base is a base class of Derived.
101
102 // Check that Derived* can be converted to Base*, ignoring
103 // accessibility. If this is possible, then Base is
104 // an unambiguous base of Derived (=> virtual bases are always
105 // unambiguous).
107
108 // Check that Base* can _not_ be converted to Derived*,
109 // again ignoring accessibility. This seals the deal:
110 // if this conversion cannot happen, it means that Base is an
111 // ambiguous base and/or it is a virtual base.
112 // But we have already established that Base is an unambiguous
113 // base, hence: Base is a virtual base.
114 std::negation<
116 >
117 >
118 >
119 > : std::true_type {};
120}
121
122template <typename Base, typename Derived>
124
125template <typename Base, typename Derived>
127#endif // __cpp_lib_is_virtual_base_of
128
129} // namespace qxp
130
131QT_END_NAMESPACE
132
133#endif // QXPTYPE_TRAITS_H
_detail::is_virtual_base_of< std::remove_cv_t< Base >, std::remove_cv_t< Derived > > is_virtual_base_of
constexpr bool is_virtual_base_of_v
constexpr bool is_detected_v
nonesuch(const nonesuch &)=delete
void operator=(const nonesuch &)=delete
~nonesuch()=delete