I'm implementing iterators for a data structure. Using a "normal" for loops yields the correct result but for some reason ranged-based for loops do not. I copied the exact syntax used by range-based for loops on cppreference and it also yields the same incorrect result but I cannot figure out why even with a debugger. My structure currently contains {5, 12, 7}.
My "normal" for loop:
for(auto begin = q.begin(); begin != q.end(); ++begin)
{
std::cout << *begin << ', ';
}
// Result: 5, 12, 7,
Syntax used by the range-based for loop:
for(auto __begin = q.begin(), __end = q.end(); __begin != __end; ++__begin)
{
std::cout << *__begin << '\n';
}
// Result: 5, 21845, 1
// Range-based for loops give the same result
State of the iterator at every step of the "normal" for loop:
{m_data = 0x7fffffffd990, m_current = 0, m_head = 0, m_tail = 0, m_end = false}
{m_data = 0x7fffffffd990, m_current = 1, m_head = 0, m_tail = 0, m_end = false}
{m_data = 0x7fffffffd990, m_current = 2, m_head = 0, m_tail = 0, m_end = false}
State of the iterator at every step of the range-based for loop:
{m_data = 0x7fffffffd990, m_current = 0, m_head = 0, m_tail = 0, m_end = false}
{m_data = 0x7fffffffd990, m_current = 1, m_head = 0, m_tail = 0, m_end = false}
{m_data = 0x7fffffffd990, m_current = 2, m_head = 0, m_tail = 0, m_end = false}
The syntax for both loops is nearly identical and the state of the iterator is identical for both. I fail to see why it doesn't yield the same result.
