diff options
author | Lars Knoll <lars.knoll@qt.io> | 2020-06-05 21:01:29 +0200 |
---|---|---|
committer | Lars Knoll <lars.knoll@qt.io> | 2020-07-08 14:13:56 +0200 |
commit | 0e2cfdedf261a9d29d7466bd26545549479d9f8a (patch) | |
tree | 8c6b3eaaad7463bf2e019b0694286aa065544138 /src/corelib/tools/qmap.h | |
parent | 16bc995fd1eba4f7485226f319e7736ca19040bc (diff) |
Constrain the comparison operators for our container classes
This had already been in very few places, where we ran into issues with
this before. More generic constraints here will significantly reduce the
amount of error messages a user has to parse in case he tries to instantiate
an operator by accident (or with a lacking comparison operator for one of
it's template arguments).
Change-Id: I1521d19c55d99732d9742402bd534c390a8e4242
Reviewed-by: Paul Wicking <paul.wicking@qt.io>
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Diffstat (limited to 'src/corelib/tools/qmap.h')
-rw-r--r-- | src/corelib/tools/qmap.h | 44 |
1 files changed, 22 insertions, 22 deletions
diff --git a/src/corelib/tools/qmap.h b/src/corelib/tools/qmap.h index a44f0088d3..d3e27f09ef 100644 --- a/src/corelib/tools/qmap.h +++ b/src/corelib/tools/qmap.h @@ -347,8 +347,28 @@ public: explicit QMap(const typename std::map<Key, T> &other); std::map<Key, T> toStdMap() const; - bool operator==(const QMap<Key, T> &other) const; - inline bool operator!=(const QMap<Key, T> &other) const { return !(*this == other); } + template <typename U = T> + QTypeTraits::compare_eq_result<U> operator==(const QMap<Key, T> &other) const + { + if (size() != other.size()) + return false; + if (d == other.d) + return true; + + const_iterator it1 = begin(); + const_iterator it2 = other.begin(); + + while (it1 != end()) { + if (!(it1.value() == it2.value()) || qMapLessThanKey(it1.key(), it2.key()) || qMapLessThanKey(it2.key(), it1.key())) + return false; + ++it2; + ++it1; + } + return true; + } + template <typename U = T> + QTypeTraits::compare_eq_result<U> operator!=(const QMap<Key, T> &other) const + { return !(*this == other); } inline qsizetype size() const { return d->size; } @@ -1045,26 +1065,6 @@ Q_INLINE_TEMPLATE typename QMap<Key, T>::iterator QMap<Key, T>::upperBound(const } template <class Key, class T> -Q_OUTOFLINE_TEMPLATE bool QMap<Key, T>::operator==(const QMap<Key, T> &other) const -{ - if (size() != other.size()) - return false; - if (d == other.d) - return true; - - const_iterator it1 = begin(); - const_iterator it2 = other.begin(); - - while (it1 != end()) { - if (!(it1.value() == it2.value()) || qMapLessThanKey(it1.key(), it2.key()) || qMapLessThanKey(it2.key(), it1.key())) - return false; - ++it2; - ++it1; - } - return true; -} - -template <class Key, class T> Q_OUTOFLINE_TEMPLATE QMap<Key, T>::QMap(const std::map<Key, T> &other) { d = QMapData<Key, T>::create(); |