-
June 21st, 2006, 10:23 PM
#1
VS2005 and the stl list::iterator
I recently brought my project over to visual studio 2005 and after compiling it, I started up to get an assertion failed error saying that list iterator is not decrementable. After some searching, I saw that apparently the list::iterator implementation in previous version would allow the user to decrement beyond the start of the list (and just ignores it), but the current version throws this exception.
Is there anyway to remove this error or to somehow get a bidirectional iterator for the stl list in VS2005? I have my own doubly linked list that I could use if I really had to, but as this is a large project containing dozens of list objects, I would prefer not to have to refactor all that code.
-
June 21st, 2006, 10:40 PM
#2
Re: VS2005 and the stl list::iterator
Originally Posted by LooselyBased
After some searching, I saw that apparently the list::iterator implementation in previous version would allow the user to decrement beyond the start of the list (and just ignores it), but the current version throws this exception.
And rightfully so. It is undefined behavior to decrement an iterator into never-never land. This is according to ANSI specifications. Your compiler may accept it, it may reject it, or at runtime you may get a variety of behaviors occuring. Bottom line, you shouldn't have coded this way.
Is there anyway to remove this error or to somehow get a bidirectional iterator for the stl list in VS2005?
What do you mean by "remove the error"? Change the compiler, or change the code?
Why not post code to what you're doing, so others can see if there is a portable way of doing what you're trying to do that won't lead to undefined behavior?
I have my own doubly linked list that I could use if I really had to, but as this is a large project containing dozens of list objects, I would prefer not to have to refactor all that code.
Again, we don't know what this code looks like so that we can help you.
However, whether you want to or not, you have to change the code. Very simply, you cannot decrement an iterator before the beginning of the list -- all the while you were relying on undefined behavior.
This is the same issue with code that assumed that iterators were pointers -- true for Visual 6.0, not true for Visual Studio 2005. Iterators are not pointers, so assuming that iterators are implemented in terms of pointers was wrong, even though VC 6.0 did implement iterators as pointers. Many programmers had to change their (erroneous) code to not assume that iterators are pointers when they ported to VS 2005. Your situation is no different.
Regards,
Paul McKenzie
-
June 22nd, 2006, 11:57 AM
#3
Re: VS2005 and the stl list::iterator
Well most of my code that uses the -- operator for the list::iterator class does so only in cases where it knows it can decrement. For example, I have a game simulator as one of my classes that takes a reference to a list and fills it with the order the players finished from last to first. So when I go to print out the scores, I start at the end and work backwards to print out the top 3 players.
Code:
struct GameWinner
{
string name;
double time;
...
};
...
GameSimulator sim;
list<GameWinner> winners;
sim.simulateTournament(winners);
list<GameWinner>::iterator cur = winners.end();
winners--;
cout << "First place: " << winners->name << " (" << winners->time << ")" << endl;
winners--;
cout << "Second place: " << winners->name << " (" << winners->time << ")" << endl;
...
-
June 22nd, 2006, 12:12 PM
#4
Re: VS2005 and the stl list::iterator
Change:
Code:
GameSimulator sim;
list<GameWinner> winners;
sim.simulateTournament(winners);
list<GameWinner>::iterator cur = winners.end();
winners--;
cout << "First place: " << winners->name << " (" << winners->time << ")" << endl;
winners--;
cout << "Second place: " << winners->name << " (" << winners->time << ")" << endl;
...
To:
Code:
GameSimulator sim;
list<GameWinner> winners;
sim.simulateTournament(winners);
list<GameWinner>::reverse_iterator cur = winners.rbegin();
winners++;
cout << "First place: " << winners->name << " (" << winners->time << ")" << endl;
winners++;
cout << "Second place: " << winners->name << " (" << winners->time << ")" << endl;
...
Viggy
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|