views:

21

answers:

1

I have the following code.

#include <set>
#include <algorithm>

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
 typedef set<long> MySet;

 MySet a;

 for( int i = 0; i < 10; ++i)
 {
  a.insert(i);
 }

 MySet::iterator start,end,last;

 start = a.begin();
 end = a.end();

 last = remove_if(start,end,bind2nd(less_equal<long>(),5));

 return 0;
}

Which under VS2005 used to compile fine. However using VS2010 I get the following error:

Error 1 error C3892: '_Next' : you cannot assign to a variable that is const c:\program files\microsoft visual studio 10.0\vc\include\algorithm

If I make the container a vector, everything is fine.

I'm guessing something has changed in the standard that I'm not aware of, can someone please shed some light on why this no longer works?

+2  A: 

A std::set always keeps its elements in sorted order. std::remove_if attempts to move the elements you don't want removed to the beginning of the collection. This would violate set's invariant of maintaining the elements in sorted order.

The code never should have worked. Older compilers might not have enforced the rules tightly enough to let you know that it wasn't supposed to work, but (apparently) your current one does.

Jerry Coffin
I suppose it's obvious when you think about it. I have been converting a massive project, which we've dragged from vc6 to vs2005 and now to vs2010, and I have many tiny little errors. Thanks for removing my mental code block. I thought vs2005 was fairly strict, I guess not as strict as vs2010.
Rich