CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Nov 2005
    Posts
    63

    Question 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.

  2. #2
    Join Date
    Apr 1999
    Posts
    27,449

    Re: VS2005 and the stl list::iterator

    Quote 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

  3. #3
    Join Date
    Nov 2005
    Posts
    63

    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;
    	...

  4. #4
    Join Date
    Feb 2002
    Posts
    4,640

    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
  •  





Click Here to Expand Forum to Full Width

Featured