On Mon, Feb 9, 2015 at 5:13 PM, Kevin Krammer <kram...@kde.org> wrote:

> On Monday, 2015-02-09, 16:52:39, PCMan wrote:
> > On Mon, Feb 9, 2015 at 7:03 AM, Kevin Krammer <kram...@kde.org> wrote:
> > > On Monday, 2015-02-09, 04:07:04, PCMan wrote:
> > > > ​OK, a list of main features I plan to use.
> > > > 1. auto
> > > > 2. nullptr
> > > > 3. new range based for() loop for iterating over containers
> > >
> > > One thing to keep looking out for with this is to be very careful when
> > > applying it on Qt containers (can trigger detach(), i.e. deep copy).
> > >
> > Thank you for the info.
> >
> > I did not expect that!
> > Previously I planed to use Qt containers with ranged for loop. :-(
> > Sounds like a bad idea, then.
>
> You can use foreach() for Qt containers and ranged-for for STL containers.
> They are potentially bad for the other use case, since they were designed
> with
> their respective containers in mind.
>
> foreach() makes a copy of the container, which is no problem for a Qt
> container since it is reference counted, but it leads to an actual copy on
> an
> STL container.
>
> ranged-for calls being/end of a container, which are only const if the
> container itself is const.
> On a non-const Qt container, the invocation of non-const methods leads to
> detach(). If the container has a reference count > 1, this will lead to an
> actual copy.
>
> The easiest way is to use the respective convenience "for", but ranged-for
> can
> be used for Qt containers if they are either ensured a reference count of
> 1 or
> the expression passed to the ranged-for is a const type.
>
>
Really thank you for the detailed and informative explanation!
This, I guess, could be alleviated partially by using move semantics
whenever possible.
Move semantics is really a better solution than reference counting IMO.
If we use it judiciously, in many of the cases the references count could
be kept 1.
Anyway, iterating over the containers will be easier with "auto".
I really hate to type QList<ClassWithLongName>::iterator so C++11 helps.
------------------------------------------------------------------------------
Dive into the World of Parallel Programming. The Go Parallel Website,
sponsored by Intel and developed in partnership with Slashdot Media, is your
hub for all things parallel software development, from weekly thought
leadership blogs to news, videos, case studies, tutorials and more. Take a
look and join the conversation now. http://goparallel.sourceforge.net/
_______________________________________________
Lxde-list mailing list
Lxde-list@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/lxde-list

Reply via email to