auto type and : in for loops

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';
Last edited on
lol i see that auto will be abused in code in the future/
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
        list<list<C_Cell*>*>::iterator colOfRowIt;
        list<C_Cell*>::iterator rowIt;
        bool a1=true;
        bool a2=true;
        bool a3=true;
        bool a4=true;
        for(colOfRowIt=colOfRow.begin();colOfRowIt!=colOfRow.end();colOfRowIt++)
        {
            if(a1)
            {
                cout<<"typeid(colOfRowIt).name()'"<<typeid(colOfRowIt).name()<<"'\n";
                cout<<"typeid((*colOfRowIt)).name()'"<<typeid((*colOfRowIt)).name()<<"'\n";
                cout<<"typeid((**colOfRowIt)).name()'"<<typeid((**colOfRowIt)).name()<<"'\n";
                a1=false;
            }
            for(rowIt=(**colOfRowIt).begin();rowIt!=(**colOfRowIt).end();rowIt++)
            {
                if(a2)
                {
                    cout<<"typeid(rowIt).name()'"<<typeid(rowIt).name()<<"'\n";
                    cout<<"typeid(*rowIt).name()'"<<typeid(*rowIt).name()<<"'\n";
                    cout<<"typeid(**rowIt).name()'"<<typeid(**rowIt).name()<<"'\n";
                    a2=false;
                }
                C_Cell&c=**rowIt;
                oStream<<c<<"\t";
            }
            oStream<<endl;
        }
        for(auto col:colOfRow)
        {
            if(a3)
            {
                cout<<"typeid(col).name()'"<<typeid(col).name()<<"'\n";
                cout<<"typeid(*col).name()'"<<typeid(*col).name()<<"'\n";
                a3=false;
            }
            for(auto row:*col)
            {
                if(a4)
                {
                    cout<<"typeid(row).name()'"<<typeid(row).name()<<"'\n";
                    cout<<"typeid(*row).name()'"<<typeid(*row).name()<<"'\n";
                    a4=false;
                }
                C_Cell&c=*row;
            }
        }

produces:

typeid(colOfRowIt).name()'St14_List_iteratorIPSt4listIP6C_CellSaIS2_EEE'
typeid((*colOfRowIt)).name()'PSt4listIP6C_CellSaIS1_EE'
typeid((**colOfRowIt)).name()'St4listIP6C_CellSaIS1_EE'
typeid(rowIt).name()'St14_List_iteratorIP6C_CellE'
typeid(*rowIt).name()'P6C_Cell'
typeid(**rowIt).name()'6C_Cell'
typeid(col).name()'PSt4listIP6C_CellSaIS1_EE'
typeid(*col).name()'St4listIP6C_CellSaIS1_EE'
typeid(row).name()'P6C_Cell'
typeid(*row).name()'6C_Cell'

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!!!
Topic archived. No new replies allowed.