[Interest] (no subject)

Constantin Makshin cmakshin at gmail.com
Mon Jun 15 06:47:04 CEST 2015


Use the range-based for operator, Luke!

And let's be honest — Qt-specific "foreach" doesn't have to work with
STL containers at all.

On 06/14/2015 10:28 AM, Prav wrote:
> 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)

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 819 bytes
Desc: OpenPGP digital signature
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20150615/d6253959/attachment.sig>


More information about the Interest mailing list