From 3b076188301905084c78ba2230906187c9965068 Mon Sep 17 00:00:00 2001 From: Mat Sutcliffe Date: Tue, 25 Dec 2018 16:07:49 +0000 Subject: [PATCH] Ref T482 Enable makeKeysRange and makePairsRange for non-const lvalues. --- src/blackmisc/range.h | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/src/blackmisc/range.h b/src/blackmisc/range.h index b18033891..9e00288e8 100644 --- a/src/blackmisc/range.h +++ b/src/blackmisc/range.h @@ -325,11 +325,19 @@ namespace BlackMisc * * This is more efficient than the keys() method of the container, as it doesn't allocate memory. */ + //! @{ template auto makeKeysRange(const T &container) { return makeRange(container.keyBegin(), container.keyEnd()); } + template + void makeKeysRange(T &container) + { + container.detach(); // http://doc.qt.io/qt-5/containers.html#implicit-sharing-iterator-problem + return makeRange(container.keyValueBegin(), container.keyValueEnd()); + } + //! @} /*! * Returns a const CRange for iterating over the keys and values of a Qt associative container. @@ -338,19 +346,19 @@ namespace BlackMisc * This is more efficient than using the keys() method of the container and thereafter looking up each key, * as it neither allocates memory, nor performs any key lookups. */ + //! @{ template auto makePairsRange(const T &container) { return makeRange(container.keyValueBegin(), container.keyValueEnd()); } - - /*! - * Keys range for a non-const lvalue would be unsafe due to iterator invalidation on detach(). - * - * \see http://doc.qt.io/qt-5/containers.html#implicit-sharing-iterator-problem - */ template - void makeKeysRange(T &container) = delete; + auto makePairsRange(T &container) + { + container.detach(); // http://doc.qt.io/qt-5/containers.html#implicit-sharing-iterator-problem + return makeRange(container.keyValueBegin(), container.keyValueEnd()); + } + //! @} /*! * Keys range for a temporary would be unsafe. @@ -358,14 +366,6 @@ namespace BlackMisc template void makeKeysRange(const T &&container) = delete; - /*! - * Pairs range for a non-const lvalue would be unsafe due to iterator invalidation on detach(). - * - * \see http://doc.qt.io/qt-5/containers.html#implicit-sharing-iterator-problem - */ - template - void makePairsRange(T &container) = delete; - /*! * Pairs range for a temporary would be unsafe. */