To loop over a 3x3 array called "a" in C++ i used the following code.
int a[3][3] {};
for(auto &b: a) {
for(auto &c: b) {
std::cout << c << std::endl;
}
}
If I had to replace the "auto", I would intuitively try
int a[3][3] {};
for(int &(b[3]): a) {
for(int &c: b) {
std::cout << c << std::endl;
}
}
But this does not work. Instead I figured out that the following works.
int a[3][3] {};
for(int (&b)[3]: a) {
for(int &c: b) {
std::cout << c << std::endl;
}
}
So the question is: Why does the latter example work?
I thought I needed a reference to an array of length 3, but instead I need an array of length 3 containing references.
int (&b)[3]This is a reference to aint[3]. It's a tricky syntax. So you do have a reference to an array of length 3.int &(b)[3]is parsed as an array of references, whileint (&b)[3]is a reference to an array of size 3. (see stackoverflow.com/questions/5724171/…)for(int i=0; i<3; i++) for(int j=0; j<3; j++) { std::cout << a[i][j] << std:endl;would have been too readable and simple. So lets try to convolute that fast, readable code into oblivion, C++1x providing all the tools. Bonus points if the machine code also turns slower.int (&b)[3]: ais still way too readable. Do you have any ideas to make it even more unreadable?