diff --git a/src/corelib/global/qglobal.h b/src/corelib/global/qglobal.h index 1663abbf82b..d1f00234eba 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 ().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 +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 QForeachContainer::type> qMakeForeachContainer(T &&t) { + warnIfContainerIsNotShared::type>(0); return QForeachContainer::type>(std::forward(t)); }