Why does operator [] work here?
int main()
{
std::array<std::size_t, 10> a;
// Example with C++23 size_t literal
for (auto i = 0uz; i != a.size(); ++i)
std::cout << (a[i] = i) << ' ';
std::cout << '\n';
// Example of decrementing loop
for (std::size_t i = a.size() ; i--;)
std::cout << a[i] << ' ';
std::cout << '\n';
}
I’m referring to these 2 lines in the decrementing loop:
for (std::size_t i = a.size() ; i--;)
std::cout << a[i] << ' ';
As I checked, a.size() returns i=10, but then a[i] is chalked up as 9 instead of 10, so a[9] is fine while a[10] obviously would not.
So, is std::array "fixing" i via the operator[] index here, under the radar? Otherwise, as far as I tried to edit the code, I can’t see how it comes together.
I made variables for the i and a[i] values and altered the for loop, but it didn't help me understand.
No.
array::operator[]uses the index it is given, and specifying an index out of bounds is Undefined Behavior as the operator does not perform any bounds checking.The "fix" is being done in the 2nd loop itself, before it calls the
operator[].In the 1st loop,
istarts at0and is incremented after being passed toa[]. The loop is doing the equivalent of this:On the first iteration,
iis 0, and on the last iterationiis 9. So, the 1st loop is accessing array elements at indexes0..9.In the 2nd loop,
istarts at10and is decremented before being passed toa[]. The loop is doing the equivalent of this:On the first iteration,
iis 10 and then decremented to 9, and on the last iterationiis 1 and decremented to 0 (the next iteration breaks the loop without accessing the array). So, the 2nd loop is accessing array elements at indexes9..0.