summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorLars Knoll <lars.knoll@qt.io>2020-06-05 12:39:58 +0200
committerLars Knoll <lars.knoll@qt.io>2020-07-08 14:13:55 +0200
commit16bc995fd1eba4f7485226f319e7736ca19040bc (patch)
tree75107f4854f97a4f0a5c340ab872d9a6a8a2d89c /src
parentb038575a8995378b07e7c82cedc219c1ae40b167 (diff)
Add type traits to safely determine the existence of comparison operators
Containers often define an operator==() or operator<() which is very useful for generic code. But those operators can usually not be instantiated if the template argument doesn't implement the operator. This sometimes leads to the compiler trying all possible template expansions and implicit conversions for the type, giving extremely long error messages. The traits support can be used to safely constrain those operators. Being able to safely detect this will also allow us to fold the comparison support that is currently a large cludge for user types directly into QMetaType. Change-Id: Ib84afb5348c3eb0be5161d6ba9d5fe237709c65f Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Diffstat (limited to 'src')
-rw-r--r--src/corelib/global/qtypeinfo.h114
-rw-r--r--src/corelib/tools/qcontainerfwd.h5
-rw-r--r--src/corelib/tools/qmap.h1
3 files changed, 119 insertions, 1 deletions
diff --git a/src/corelib/global/qtypeinfo.h b/src/corelib/global/qtypeinfo.h
index 121c9fdcd8..8b31fb4a81 100644
--- a/src/corelib/global/qtypeinfo.h
+++ b/src/corelib/global/qtypeinfo.h
@@ -39,6 +39,8 @@
****************************************************************************/
#include <QtCore/qglobal.h>
+#include <QtCore/qcontainerfwd.h>
+#include <variant>
#ifndef QTYPEINFO_H
#define QTYPEINFO_H
@@ -322,5 +324,117 @@ Q_DECLARE_TYPEINFO(wchar_t, Q_RELOCATABLE_TYPE);
# endif
#endif // Qt 6
+namespace QTypeTraits
+{
+
+/*
+ The templates below aim to find out whether one can safely instantiate an operator==() or
+ operator<() for a type.
+
+ This is tricky for containers, as most containers have unconstrained comparison operators, even though they
+ rely on the corresponding operators for its content.
+ This is especially true for all of the STL template classes that have a comparison operator defined, and
+ leads to the situation, that the compiler would try to instantiate the operator, and fail if any
+ of its template arguments does not have the operator implemented.
+
+ The code tries to cover the relevant cases for Qt and the STL, by checking (recusrsively) the value_type
+ of a container (if it exists), and checking the template arguments of pair, tuple and variant.
+*/
+namespace detail {
+
+// find out whether T has a value_type typedef
+// this is required to check the value type of containers for the existence of the comparison operator
+template <typename, typename = void>
+struct has_value_type : std::false_type {};
+template <typename T>
+struct has_value_type<T, std::void_t<typename T::value_type>> : std::true_type {};
+
+// Checks the existence of the comparison operator for the class itself
+template <typename, typename = void>
+struct has_operator_equal : std::false_type {};
+template <typename T>
+struct has_operator_equal<T, std::void_t<decltype(bool(std::declval<const T&>() == std::declval<const T&>()))>>
+ : std::true_type {};
+
+// Two forward declarations
+template<typename T, bool = has_value_type<T>::value>
+struct expand_operator_equal_container;
+template<typename T>
+struct expand_operator_equal_tuple;
+
+// the entry point for the public method
+template<typename T>
+using expand_operator_equal = expand_operator_equal_container<T>;
+
+// if T doesn't have a value_type member check if it's a tuple like object
+template<typename T, bool>
+struct expand_operator_equal_container : expand_operator_equal_tuple<T> {};
+// if T::value_type exists, check first T::value_type, then T itself
+template<typename T>
+struct expand_operator_equal_container<T, true> :
+ std::conjunction<expand_operator_equal<typename T::value_type>, expand_operator_equal_tuple<T>> {};
+
+// recursively check the template arguments of a tuple like object
+template<typename ...T>
+using expand_operator_equal_recursive = std::conjunction<expand_operator_equal<T>...>;
+
+template<typename T>
+struct expand_operator_equal_tuple : has_operator_equal<T> {};
+template<typename T1, typename T2>
+struct expand_operator_equal_tuple<std::pair<T1, T2>> : expand_operator_equal_recursive<T1, T2> {};
+template<typename ...T>
+struct expand_operator_equal_tuple<std::tuple<T...>> : expand_operator_equal_recursive<T...> {};
+template<typename ...T>
+struct expand_operator_equal_tuple<std::variant<T...>> : expand_operator_equal_recursive<T...> {};
+
+// the same for operator<(), see above for explanations
+template <typename, typename = void>
+struct has_operator_less_than : std::false_type{};
+template <typename T>
+struct has_operator_less_than<T, std::void_t<decltype(bool(std::declval<const T&>() < std::declval<const T&>()))>>
+ : std::true_type{};
+
+template<typename T, bool = has_value_type<T>::value>
+struct expand_operator_less_than_container;
+template<typename T>
+struct expand_operator_less_than_tuple;
+
+template<typename T>
+using expand_operator_less_than = expand_operator_less_than_container<T>;
+
+template<typename T, bool>
+struct expand_operator_less_than_container : expand_operator_less_than_tuple<T> {};
+template<typename T>
+struct expand_operator_less_than_container<T, true> :
+ std::conjunction<expand_operator_less_than<typename T::value_type>, expand_operator_less_than_tuple<T>> {};
+
+template<typename ...T>
+using expand_operator_less_than_recursive = std::conjunction<expand_operator_less_than<T>...>;
+
+template<typename T>
+struct expand_operator_less_than_tuple : has_operator_less_than<T> {};
+template<typename T1, typename T2>
+struct expand_operator_less_than_tuple<std::pair<T1, T2>> : expand_operator_less_than_recursive<T1, T2> {};
+template<typename ...T>
+struct expand_operator_less_than_tuple<std::tuple<T...>> : expand_operator_less_than_recursive<T...> {};
+template<typename ...T>
+struct expand_operator_less_than_tuple<std::variant<T...>> : expand_operator_less_than_recursive<T...> {};
+
+}
+
+template<typename T>
+struct has_operator_equal : detail::expand_operator_equal<T> {};
+template<typename T>
+constexpr bool has_operator_equal_v = has_operator_equal<T>::value;
+
+template<typename T>
+struct has_operator_less_than : detail::expand_operator_less_than<T> {};
+template<typename T>
+constexpr bool has_operator_less_than_v = has_operator_less_than<T>::value;
+
+
+}
+
+
QT_END_NAMESPACE
#endif // QTYPEINFO_H
diff --git a/src/corelib/tools/qcontainerfwd.h b/src/corelib/tools/qcontainerfwd.h
index 715583e03b..4aec3574ff 100644
--- a/src/corelib/tools/qcontainerfwd.h
+++ b/src/corelib/tools/qcontainerfwd.h
@@ -42,8 +42,11 @@
#include <QtCore/qglobal.h>
-QT_BEGIN_NAMESPACE
+// std headers can unfortunately not be forward declared
+#include <tuple>
+#include <variant>
+QT_BEGIN_NAMESPACE
template <class Key, class T> class QCache;
template <class Key, class T> class QHash;
diff --git a/src/corelib/tools/qmap.h b/src/corelib/tools/qmap.h
index 848ba5c4d4..a44f0088d3 100644
--- a/src/corelib/tools/qmap.h
+++ b/src/corelib/tools/qmap.h
@@ -567,6 +567,7 @@ public:
// STL compatibility
typedef Key key_type;
typedef T mapped_type;
+ typedef T value_type;
typedef qptrdiff difference_type;
typedef qsizetype size_type;
inline bool empty() const { return isEmpty(); }