Just want to throw in my "foreach key/value" loop implementation, as an 
extension of "foreach", which I did years ago just as a proof of 
concept.

http://codereview.stackexchange.com/questions/11681/

It allows you to do:

     foreachkv(auto key, auto value, map) {
         // do sth with key / value
     }

Its implementation follows straight out of how Q_FOREACH is implemented, 
just adding another for-loop. I only implemented the GCC version back 
then, though.


On 20.02.2015 18:53, Matthew Woehlke wrote:
> On 2015-02-20 07:10, Иван Комиссаров wrote:
>> Sorry for interupting the discussion, but i saw mentioning of a
>> range-based-for, so i have a question. std::map/unordered_map uses
>> std::pair as a value type, and map::iterator::operator* returns 
>> reference
>> to a pair, while Qt doesn't have an underlying struct and operator* 
>> returns
>> ref to T (without a Key). So, using range-based-for (and foreach) with 
>> Qt
>> containers doesn't allow to have an access to a key.
>> Should this behavior be changed in the future (yes, this breaks source
>> compatibility)?
> 
> No. No need. (Also... note that foreach doesn't give you keys either.)
> 
> You can instead wrap the container in an iterator wrapper that gives 
> you
> iterators rather than values. (I have code somewhere, but not sure I
> have permission to share it.) No SIC, and you can still iterate 
> directly
> over values.
> 
> It's not entirely unlike the trick to do:
> 
>   for (auto const i : qtIndexRange(5))
> 
> ...and looks like:
> 
>   for (auto const i : qtEnumerate(map))
> 
> Maybe it would be nice for Qt to provide one or both of these?
_______________________________________________
Development mailing list
Development@qt-project.org
http://lists.qt-project.org/mailman/listinfo/development

Reply via email to