I am running into strange behavior when using std::list<std::array<T>>::iterator.
I'm trying to run the following code:
#include <iostream>
#include <list>
#include <array>
#include <algorithm>
using namespace std;
struct S {
uint64_t x;
};
#define N 10000
typedef array<S, N> block;
typedef list<block>::iterator it;
int main() {
list<block> l;
l.emplace_back();
block& b = l.front();
it i = l.begin();
l.emplace_back();
std::cout << i->data() << " " << b.data() << "\n";
b = *(++i);
std::cout << i->data() << " " << b.data() << "\n";
}
It gives me the following output:
0x55c2b63cbec0 0x55c2b63cbec0
0x55c2b63df760 0x55c2b63cbec0
When accessing the .data() through the iterator, it correctly points to the next element. However, dereferencing the incremented iterator gives me a reference to the same old element. I have been scratching my head over this for some hours now. Am I doing something wrong or misunderstanding how iterators work?
When I try the same code but with just list<int>, it works as expected.
list<int> l;
l.emplace_back(0);
int& b = l.front();
list<int>::iterator i = l.begin();
l.emplace_back(1);
std::cout << *i << " " << b << "\n";
b = *(++i);
std::cout << *i << " " << b << "\n";
Produces the expected output:
0 0
1 1