std::advance too much?

G

Gernot Frisch

Hi,

what doi I get when I advance too much?

std::list<int> L;
L.push_back(4);
std::list<int>::iterator it = L.begin;
std::advance(it, 3);
for(; it!=L.end(); ++it)
{
// do I get here?!
}



--
-Gernot
int main(int argc, char** argv) {printf
("%silto%c%cf%cgl%ssic%ccom%c", "ma", 58, 'g', 64, "ba", 46, 10);}

________________________________________
Looking for a good game? Do it yourself!
GLBasic - you can do
www.GLBasic.com
 
J

John Harrison

Gernot Frisch said:
Hi,

what doi I get when I advance too much?

std::list<int> L;
L.push_back(4);
std::list<int>::iterator it = L.begin;
std::advance(it, 3);
for(; it!=L.end(); ++it)
{
// do I get here?!
}

What you get is undefined behaviour.

How about this (untested code)

template <class Iter, class Size>
void test_and_advance(Iter& curr, Iter end, Size n)
{
while (curr != end && n > 0)
{
++curr;
--n;
}
}

std::list<int>::iterator it = L.begin();
test_and_advance(it, L.end(), 3);
for(; it!=L.end(); ++it)
{

john
 
G

Gernot Frisch

John Harrison said:
What you get is undefined behaviour.

Ah. Thank you. Why doesn't this:
it = L.end(); ++it;
Make it == L.end()??

That would be obvious. Or even better - why doesn't "illegal" return
"NULL" in std::containers?
How about this (untested code)

Yes, I'll do that. I'll write my own advance function...
Thank you,
Gernot
 
R

Rob Williscroft

Gernot Frisch wrote in in comp.lang.c++:
Ah. Thank you. Why doesn't this:
it = L.end(); ++it;
Make it == L.end()??

Because it isn't in the requirements for (std) iterators that you can
call ++ on a pass-the-end iterator (which is what end() returns).

So implementing this behaviour (or any other) is useless for people
writing portable code.

That would be obvious. Or even better - why doesn't "illegal" return
"NULL" in std::containers?

std containers are for better or worse modeled on array's, and with
an array you can have a 1 past the end iterator (pointer) that can
be decremented. Having a singular (NULL) end() iterator couldn't
support that property.

Also pointers (which are iterators over array's) can't support
this beahviour.

Rob.
 
J

John Harrison

Gernot Frisch said:
Ah. Thank you. Why doesn't this:
it = L.end(); ++it;
Make it == L.end()??

To implement that would require that an iterator know what container it is
iterating over, so that it could check the container to see if it had
reached the end. It would certainly be possible to implement iterators like
that but the standard committee decided not to require that behaviour, I
guess for efficiency reasons.
That would be obvious. Or even better - why doesn't "illegal" return
"NULL" in std::containers?

I don't understand what you mean here.

john
 
T

tom_usenet

Ah. Thank you. Why doesn't this:
it = L.end(); ++it;
Make it == L.end()??

That would be obvious. Or even better - why doesn't "illegal" return
"NULL" in std::containers?

Performance reasons. It certainly can't be done if the iterator is
actually a T*.

A good compromise is to have the poor performance in debug builds with
the checks removed for the release. See the new iterator debugging
feature from Dinkumware: www.dinkumware.com. For a free alternative,
STLport offers a debugging mode: www.stlport.com.

Tom
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,769
Messages
2,569,577
Members
45,052
Latest member
LucyCarper

Latest Threads

Top