Optimize Q_FOREACH for rvalues

Add an rvalue overload of the QForeachContainer ctor to allow moving
rvalues into the internal container copy.

This does not change the semantics of Q_FOREACH. It is just an
optimization.

Port to NSDMI to minimize code duplication.

Costs ~1.3KiB across all libraries and plugins in a QtBase Linux
build (optimized GCC 6.1 AMD64).

Change-Id: I180e35ecab68aa1d37773b3546787481bb5515a2
Reviewed-by: Olivier Goffart (Woboq GmbH) <ogoffart@woboq.com>
bb10
Marc Mutz 2016-08-31 13:00:13 +02:00
parent 806b45e7c7
commit 6549bd383d
1 changed files with 4 additions and 3 deletions

View File

@ -943,10 +943,11 @@ template <typename T>
class QForeachContainer {
QForeachContainer &operator=(const QForeachContainer &) Q_DECL_EQ_DELETE;
public:
inline QForeachContainer(const T& t) : c(t), i(c.begin()), e(c.end()), control(1) { }
QForeachContainer(const T &t) : c(t) {}
QForeachContainer(T &&t) : c(std::move(t)) {}
const T c;
typename T::const_iterator i, e;
int control;
typename T::const_iterator i = c.begin(), e = c.end();
int control = 1;
};
// Explanation of the control word: