Hi,
I'm interested in the auto type and for loops, but I'm not clear about what it does exactly.
Here's and example of some code the old way:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
//list<list<C_Cell*>*>colOfRow;//filled elsewhere but shown to clarify the type
list<list<C_Cell*>*>::iterator colOfRowIt;
list<C_Cell*>::iterator rowIt;
for(colOfRowIt=colOfRow.begin();colOfRowIt!=colOfRow.end();colOfRowIt++)
{
//list<C_Cell*>*rp=*colOfRowIt;//expansion to clarify type below
//for(rowIt=rp->begin();rowIt!=rp->end();rowIt++)//expansion to clarify type
for(rowIt=(*colOfRowIt)->begin();rowIt!=(*colOfRowIt)->end();rowIt++)
{
// C_Cell*cp=*rowIt;//expansion to clarify type below
// C_Cell&c=*cp;//expansion to clarify type below
C_Cell&c=**rowIt;
}
}
So my question is how to convert this to auto
My attempt that compiles:
1 2 3 4 5 6 7
for(auto &col:colOfRow)
{
for(auto& row:*col)
{
C_Cell&c=*row;//but shouldn't this be C_Cell&c=**row;? (doesn't compile)
}
}
Is it not possible to do auto here because of pointers within iterators?
Or it is possible and just seems buggy?
Is there a way to find out what auto type is interpreted as explicitly?
list of pointers to lists looks very bizarre, but nevertheless, your row is a reference to whatever begin(*col) dereferences to, that is, the pointer to C_Cell.
You could think of it as automatically dereferenced iterator.
Is there a way to find out what auto type is interpreted as explicitly?
Same as any other type, cout << typeid(row).name() << '\n';
So all is well.
I think maybe I just confused myself by doing (*colOfRowIt)->begin() instead of (**colOfRowIt).begin().
Now the dereferencing seems consistent and I get it.
This is really cool!!!