The Qt's documentation says the following:
Thanks to implicit sharing, it is very inexpensive for a function to return a container per value. The Qt API contains dozens of functions that return a QList or QStringList per value (e.g., QSplitter::sizes()). If you want to iterate over these using an STL iterator, you should always take a copy of the container and iterate over the copy. For example:
// RIGHT
const QList<int> sizes = splitter->sizes();
QList<int>::const_iterator i;
for (i = sizes.begin(); i != sizes.end(); ++i)
...
// WRONG
QList<int>::const_iterator i;
for (i = splitter->sizes().begin();
i != splitter->sizes().end(); ++i)
...
What will happen if the 'Wrong' method is applied?
The two calls to splitter->sizes()
produce two distinct copies of the container. Since begin()
comes from one and end()
from the other, they don't form a valid range. The loop would then walk off the end of the first container, into the land of undefined behavior.
Range-based loop would work just fine though: for (int size: splitter->sizes()) { ... }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With