On Tue, Apr 28, 2015 at 04:05:10PM +0100, Alex Valavanis wrote:
== 2. Range-based for-loops ==
Often, we just want to iterate over the entire contents of a container. The syntax is much cleaner in C++11:
for(std::vectorInkscape::SnapCandidatePoint::iterator i = _all_snap_sources_sorted.begin(); i != _all_snap_sources_sorted.end(); ++i){ // whatever }
becomes...
for(auto &i : _all_snap_sources_sorted){ // whatever }
Some of Inkscape's loops (very few) modify the container that is looped over. In that case, the two are not equivalent. IIRC,
for (auto &i : container)
is syntactic sugar for something like:
for (container::iterator i = begin(container), e = end(container); i != e; ++i)
Note that end(container) is evaluated only once, instead of on every iteration. (Independent of C++11, if you write a new loop, in my opinion it's better to use this notation instead of "i != end(container)".)
A loop like this: for (...container...) { ... container.push_back(blah); ... } will do something different with ranged-for notation. Regardless, such a loop is pretty confusing for most and should be rewritten anyway. I remember a bug involving one such loop, perhaps it was the only one in the program.
-Johan