Use std::vector range ctor in QVector::toStdVector()

There are three reasons to do so:

1. This could be more efficient, depending on the STL implementation.

2. By using QTypedArrayData iterators (T*) instead of QVector ones,
   we actually invoke the non-templated range ctor of std::vector,
   at least in the common case that std::vector<T>::const_iterator
   is also const T*.

3. The change turns a former NRVO return into a RVO one, potentially
   allowing more compilers to perform the copy elision.

Change-Id: I70b35aaeae70ba06a971a36b8b1b1da997e8094f
Reviewed-by: Olivier Goffart <ogoffart@woboq.com>
This commit is contained in:
Marc Mutz 2014-03-25 00:28:19 +01:00
parent b983818219
commit e3e4fe7910

View File

@ -256,7 +256,7 @@ public:
static inline QVector<T> fromStdVector(const std::vector<T> &vector) static inline QVector<T> fromStdVector(const std::vector<T> &vector)
{ QVector<T> tmp; tmp.reserve(int(vector.size())); std::copy(vector.begin(), vector.end(), std::back_inserter(tmp)); return tmp; } { QVector<T> tmp; tmp.reserve(int(vector.size())); std::copy(vector.begin(), vector.end(), std::back_inserter(tmp)); return tmp; }
inline std::vector<T> toStdVector() const inline std::vector<T> toStdVector() const
{ std::vector<T> tmp; tmp.reserve(size()); std::copy(constBegin(), constEnd(), std::back_inserter(tmp)); return tmp; } { return std::vector<T>(d->begin(), d->end()); }
private: private:
friend class QRegion; // Optimization for QRegion::rects() friend class QRegion; // Optimization for QRegion::rects()