According to the answers in this and this question, the C++ standard states in § 23.2.1 that end()
is of constant time complexity for all stl containers.
If I am understanding correctly:
std::forward_list
only knows about it's first element, and each of the list entries only know about the next element.- lists are non-contiguous in memory
a.begin() == a.end()
is true for empty containersa
end()
is supposed to be an iterator pointing to 'one past the end of a container'
So while working on some loops over forward_list
s, I was wondering:
How can end() be of constant time complexity (i.e. not advance to 'one past the end') in case of forward_list?
I looked in forward_list.cpp
and found the declaration
iterator end() _NOEXCEPT
{return iterator(nullptr);}
That makes sense for the constant time requirement, but not for the - admittedly vage - rule corresponding to point 4 above.
So some questions remain:
- What is 'one past the end' supposed to mean for non-contiguous storage?
- How does
nullptr
fit the definition of 'one past the end'? - How is
MyForwardList.begin() == MyForwardList.end()
true ifMyForwardList
is empty? - Why isn't
end()
always defined asnullptr
?