From bd84e8d63e6fc2e05df21e8c60d56c01b5fa0674 Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Mon, 8 Jul 2019 21:25:55 +0200 Subject: QHash/QMap: don't return const from value(), key() ... it breaks move semantics. We can change these, since they're templates and a short survey shows that no-one in Qt was crazy enough to inherit an exported class from QHash or QMap. Otherwise this would be BiC on MSVC, which encodes the return type. There's also no safety benefit here, as none of the overloads returns by reference, so users cannot expect map.value(key).mutate() to have an effect on the element in the container. In this, key() and value() differ from op[], which also returns const, but whose overload returns a reference. op[] is therefore not proposed here. [ChangeLog][QtCore][QHash/QMultiHash/QMap/QMultiMap] The value() and key() member functions now return T (was: const T), enabling move semantics on their return values. Change-Id: I0e5f53f9834caad458e3bde27f1daacbb4bac71b Reviewed-by: Lars Knoll Reviewed-by: Volker Hilsheimer --- src/corelib/tools/qhash.cpp | 4 ++-- src/corelib/tools/qhash.h | 16 ++++++++-------- src/corelib/tools/qmap.cpp | 2 +- src/corelib/tools/qmap.h | 8 ++++---- 4 files changed, 15 insertions(+), 15 deletions(-) diff --git a/src/corelib/tools/qhash.cpp b/src/corelib/tools/qhash.cpp index 7ebc374d9f..1e5af7e0ce 100644 --- a/src/corelib/tools/qhash.cpp +++ b/src/corelib/tools/qhash.cpp @@ -1449,7 +1449,7 @@ uint qHash(long double key, uint seed) noexcept \sa count(), QMultiHash::contains() */ -/*! \fn template const T QHash::value(const Key &key) const +/*! \fn template T QHash::value(const Key &key) const Returns the value associated with the \a key. @@ -1461,7 +1461,7 @@ uint qHash(long double key, uint seed) noexcept \sa key(), values(), contains(), operator[]() */ -/*! \fn template const T QHash::value(const Key &key, const T &defaultValue) const +/*! \fn template T QHash::value(const Key &key, const T &defaultValue) const \overload If the hash contains no item with the given \a key, the function returns diff --git a/src/corelib/tools/qhash.h b/src/corelib/tools/qhash.h index c87cf56988..d03f301306 100644 --- a/src/corelib/tools/qhash.h +++ b/src/corelib/tools/qhash.h @@ -298,10 +298,10 @@ public: T take(const Key &key); bool contains(const Key &key) const; - const Key key(const T &value) const; - const Key key(const T &value, const Key &defaultKey) const; - const T value(const Key &key) const; - const T value(const Key &key, const T &defaultValue) const; + Key key(const T &value) const; + Key key(const T &value, const Key &defaultKey) const; + T value(const Key &key) const; + T value(const Key &key, const T &defaultValue) const; T &operator[](const Key &key); const T operator[](const Key &key) const; @@ -631,7 +631,7 @@ Q_INLINE_TEMPLATE QHash &QHash::operator=(const QHash &other) } template -Q_INLINE_TEMPLATE const T QHash::value(const Key &akey) const +Q_INLINE_TEMPLATE T QHash::value(const Key &akey) const { Node *node; if (d->size == 0 || (node = *findNode(akey)) == e) { @@ -642,7 +642,7 @@ Q_INLINE_TEMPLATE const T QHash::value(const Key &akey) const } template -Q_INLINE_TEMPLATE const T QHash::value(const Key &akey, const T &adefaultValue) const +Q_INLINE_TEMPLATE T QHash::value(const Key &akey, const T &adefaultValue) const { Node *node; if (d->size == 0 || (node = *findNode(akey)) == e) { @@ -679,13 +679,13 @@ Q_OUTOFLINE_TEMPLATE QList QHash::keys(const T &avalue) const } template -Q_OUTOFLINE_TEMPLATE const Key QHash::key(const T &avalue) const +Q_OUTOFLINE_TEMPLATE Key QHash::key(const T &avalue) const { return key(avalue, Key()); } template -Q_OUTOFLINE_TEMPLATE const Key QHash::key(const T &avalue, const Key &defaultValue) const +Q_OUTOFLINE_TEMPLATE Key QHash::key(const T &avalue, const Key &defaultValue) const { const_iterator i = begin(); while (i != end()) { diff --git a/src/corelib/tools/qmap.cpp b/src/corelib/tools/qmap.cpp index 25ea5f25d7..e2582ccdf4 100644 --- a/src/corelib/tools/qmap.cpp +++ b/src/corelib/tools/qmap.cpp @@ -704,7 +704,7 @@ void QMapDataBase::freeData(QMapDataBase *d) \sa count(), QMultiMap::contains() */ -/*! \fn template const T QMap::value(const Key &key, const T &defaultValue) const +/*! \fn template T QMap::value(const Key &key, const T &defaultValue) const Returns the value associated with the key \a key. diff --git a/src/corelib/tools/qmap.h b/src/corelib/tools/qmap.h index 6081019294..be8ba6c4ba 100644 --- a/src/corelib/tools/qmap.h +++ b/src/corelib/tools/qmap.h @@ -364,8 +364,8 @@ public: T take(const Key &key); bool contains(const Key &key) const; - const Key key(const T &value, const Key &defaultKey = Key()) const; - const T value(const Key &key, const T &defaultValue = T()) const; + Key key(const T &value, const Key &defaultKey = Key()) const; + T value(const Key &key, const T &defaultValue = T()) const; T &operator[](const Key &key); const T operator[](const Key &key) const; @@ -630,7 +630,7 @@ QT_WARNING_PUSH QT_WARNING_DISABLE_CLANG("-Wreturn-stack-address") template -Q_INLINE_TEMPLATE const T QMap::value(const Key &akey, const T &adefaultValue) const +Q_INLINE_TEMPLATE T QMap::value(const Key &akey, const T &adefaultValue) const { Node *n = d->findNode(akey); return n ? n->value : adefaultValue; @@ -979,7 +979,7 @@ Q_OUTOFLINE_TEMPLATE QList QMap::keys(const T &avalue) const } template -Q_OUTOFLINE_TEMPLATE const Key QMap::key(const T &avalue, const Key &defaultKey) const +Q_OUTOFLINE_TEMPLATE Key QMap::key(const T &avalue, const Key &defaultKey) const { const_iterator i = begin(); while (i != end()) { -- cgit v1.2.3