diff options
author | Olivier Goffart <ogoffart@woboq.com> | 2018-10-29 14:33:27 +0100 |
---|---|---|
committer | Giuseppe D'Angelo <giuseppe.dangelo@kdab.com> | 2020-11-02 14:28:17 +0100 |
commit | 1926e09b32fb6bd6ada6951c36cd337561964b79 (patch) | |
tree | aadc15f890c8574d1a49cecebc2b9b288267e749 /src/corelib/global | |
parent | d76b9d86e85ed7c0705209caca40b7a0efc71e14 (diff) |
Warn if Q_FOREACH is used with a non-shared container
Show a deprecation warning if a non shared container is used within
Q_FOREACH, because it would make an expensive copy of the container
Change-Id: I70cfd789b5b8d9f5b1bd6e0a57e5e968e1c6a0ca
Reviewed-by: Giuseppe D'Angelo <giuseppe.dangelo@kdab.com>
Diffstat (limited to 'src/corelib/global')
-rw-r--r-- | src/corelib/global/qglobal.h | 14 |
1 files changed, 14 insertions, 0 deletions
diff --git a/src/corelib/global/qglobal.h b/src/corelib/global/qglobal.h index 1663abbf82..d1f00234eb 100644 --- a/src/corelib/global/qglobal.h +++ b/src/corelib/global/qglobal.h @@ -1083,9 +1083,23 @@ public: int control = 1; }; +// Containers that have a detach function are considered shared, and are OK in a foreach loop +template <typename T, typename = decltype(std::declval<T>().detach())> +inline void warnIfContainerIsNotShared(int) {} + +#if QT_DEPRECATED_SINCE(6, 0) +// Other containers will copy themselves if used in foreach, this use is deprecated +template <typename T> +QT_DEPRECATED_VERSION_X_6_0("Do not use foreach/Q_FOREACH with containers which are not implicitly shared. " + "Prefer using a range-based for loop with these containers: `for (const auto &it : container)`, " + "keeping in mind that range-based for doesn't copy the container as Q_FOREACH does") +inline void warnIfContainerIsNotShared(...) {} +#endif + template<typename T> QForeachContainer<typename std::decay<T>::type> qMakeForeachContainer(T &&t) { + warnIfContainerIsNotShared<typename std::decay<T>::type>(0); return QForeachContainer<typename std::decay<T>::type>(std::forward<T>(t)); } |