vendredi 22 février 2019

Why I cannot use a decltype in range-for with multi-dimension arrays?

I have a problem here. I am trying to use decltype in range-for loop for using multi-dimension array:

    int a[][4]{
    {0, 1, 2, 3 },
    {4, 5, 6, 7 },
    {8, 9, 10, 11}
};

for (auto& row : a) { // reference is needed here to prevent array decay to pointer
    cout << "{";
    for (auto col : row)
        cout << col << ", ";
    cout << "}" << endl;
}


decltype (*a) row{ *a};
cout << sizeof(row) << endl;
cout << typeid(row).name() << endl;

//  for (decltype(*a) row : *a) {
//      for (int col : row)
//          cout << col << ", ";
//      cout << endl;
//  }

With auto I can easily iterate over the the array But with decltype it doesn't work for me.

What I get above if I uncomment the code is: cannot convert from int to int(&)[4].

Aucun commentaire:

Enregistrer un commentaire