[Qt-creator] C++11 in Creator's source: Update required features

Daniel Teske Daniel.Teske at theqtcompany.com
Mon Mar 9 12:14:04 CET 2015


> If anyone wants to propose a coding style, please come forward.

** Range-based for **
I'd like to propose the following guideline:

Do not use the C++11 range based for loop unless your loop modifies the 
contents of the container. Prefer to use Qt's foreach macro for read-only 
iteration.


Reason:
The range-based for loop is using the helper functions std::begin() and 
std::end, which unless overloaded call container.begin() and container.end().

Which for Qt's container classes call detach unless the container is const.

Overloading std::begin() and std::end() to return const_iterators doesn't 
work, since you can't then no longer modify the container's contents.

Thus, the range-based for loop has worse performance for read-only iteration 
unless:

- The container is const
- The container is unshared

This makes imho the range-based for loop to error-prone for read-only 
iteration. I'd like to allow the range-based for loop for modification, since 
that's something that simply isn't possible with qt's foreach macro. 

But, I would also agree to a coding guideline of discouraging the range based 
for loop completely.

daniel



More information about the Qt-creator mailing list