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 = is_detected<Op, Args...>::value;
58
59
60// qxp::is_virtual_base_of_v<B, D> is true if and only if B is a virtual base class of D.
61// Just like is_base_of:
62// * only works on complete types;
63// * B and D must be class types;
64// * ignores cv-qualifications;
65// * B may be inaccessibile.
66
67#ifdef __cpp_lib_is_virtual_base_of
70#else
71namespace _detail {
72 // Check that From* can be converted to To*, ignoring accessibility.
73 // This can be done using a C cast (see [expr.cast]/4).
75QT_WARNING_DISABLE_GCC("-Wold-style-cast")
76QT_WARNING_DISABLE_CLANG("-Wold-style-cast")
77 template <typename From, typename To>
79 (To *)std::declval<From *>()
80 );
82
83 template <typename Base, typename Derived, typename = void>
85
86 template <typename Base, typename Derived>
87 struct is_virtual_base_of<
91 // Base is a base class of Derived.
93
94 // Check that Derived* can be converted to Base*, ignoring
95 // accessibility. If this is possible, then Base is
96 // an unambiguous base of Derived (=> virtual bases are always
97 // unambiguous).
99
100 // Check that Base* can _not_ be converted to Derived*,
101 // again ignoring accessibility. This seals the deal:
102 // if this conversion cannot happen, it means that Base is an
103 // ambiguous base and/or it is a virtual base.
104 // But we have already established that Base is an unambiguous
105 // base, hence: Base is a virtual base.
106 std::negation<
108 >
109 >
110 >
111 > : std::true_type {};
112}
113
114template <typename Base, typename Derived>
115using is_virtual_base_of = _detail::is_virtual_base_of<std::remove_cv_t<Base>, std::remove_cv_t<Derived>>;
116
117template <typename Base, typename Derived>
119#endif // __cpp_lib_is_virtual_base_of
120
121} // namespace qxp
122
123QT_END_NAMESPACE
124
125#endif // QXPTYPE_TRAITS_H
decltype((To *) std::declval< From * >()) is_virtual_base_conversion_test
constexpr bool is_virtual_base_of_v
constexpr bool is_detected_v
nonesuch(const nonesuch &)=delete
void operator=(const nonesuch &)=delete
~nonesuch()=delete