mardi 27 novembre 2018

Type inference of 'auto' when using range-for loop in multidimensional arrays

After int ia[3][4]{}. Then I run:

for(auto row : ia) // row should be type int*
    for(int *j = std::begin(*row); j!= end(*row); ++j) // error!!
        std::cout << *j << std::endl;

According to C++ primer 5th:

Because row is not a reference, when the compiler
initializes row it will convert each array element (like any other 
object of array type)
to a pointer to that array’s first element

So if row is a pointer to ia's first element, then why error happens?

Many thanks!

Aucun commentaire:

Enregistrer un commentaire