Why std::remove takes const version of iterators? [duplicate]

This question already has an answer here:

I am using Visual Studio 2013 to compile very simple code:

std::set<int> a{ 1, 2, 3 };
std::remove(a.begin(), a.end(), 3);

I expect this can’t go wrong but I am surprised. Error emits:

Error   1   error C3892: '_Next' : you cannot assign to a variable that is const    c:program files (x86)microsoft visual studio 12.0vcincludealgorithm

How could this be? a is a non-const std::set. std::remove() moves the element of it and seems perfectly legal.

In VS 2008, below similar code compiles with no error:

std::set<int> a;
std::remove(a.begin(), a.end(), 3);

Source: c++

Leave a Reply