[Interest] (no subject)
Prav
pr12og2 at programist.ru
Sun Jun 14 09:28:08 CEST 2015
Why it is done so that foreach statement is doing copying of
container?
I can not imagine good cases of using current container-copy feature of foreach ... to be able to modify container while looping through its hiddenly copied duplicate ... well ... this is something ... let's say special ;)
This copying happens because in qglobal.h there is "const T c;":
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) { }
const T c;
typename T::const_iterator i, e;
int control;
};
As soon as foreach is intended to not modify container I see no point in copying it. Change in as easy as saying "const T &c;" instead of current "const T c;"
Although current implementation of iterator-loop have the right to exist but in this case I would expect it to co-exist with another iterator-loop which works in more straightforward for developers way (with just const-ref to container).
P.S. And yes ... I now about implicit sharing of Qt containers.
But Qt-containers tries to be stl-compatible and if Qt-developer is used to use foreach for Qt-containers it could be non-obvious for him/her that for stl-container the container data copying is happening every time foreach statement is used (as soon as stl-containers does not do implicit sharing)
More information about the Interest
mailing list