QList: improve the range constructors

In case of forward iterators, call std::distance just once and not
twice. In case of non-forward iterators, don't call
reserveIfForwardIterator -- as the name says, it doesn't make sense
on non-forward iterators.

Change-Id: I7e6a603205286c05f7bc7c47fd1f1e0d92705b20
Reviewed-by: Lars Knoll <lars.knoll@qt.io>
This commit is contained in:
Giuseppe D'Angelo 2020-06-24 20:34:15 +02:00
parent 95326a2977
commit 2e51686746

View File

@ -144,17 +144,17 @@ public:
}
template <typename InputIterator, QtPrivate::IfIsForwardIterator<InputIterator> = true>
QList(InputIterator i1, InputIterator i2)
: d(Data::allocate(std::distance(i1, i2)))
{
if (std::distance(i1, i2))
const auto distance = std::distance(i1, i2);
if (distance) {
d = DataPointer(Data::allocate(distance));
d->copyAppend(i1, i2);
}
}
template <typename InputIterator, QtPrivate::IfIsNotForwardIterator<InputIterator> = true>
QList(InputIterator i1, InputIterator i2)
: QList()
{
QtPrivate::reserveIfForwardIterator(this, i1, i2);
std::copy(i1, i2, std::back_inserter(*this));
}