As per this question, and assuming I have a mutable reference to the container itself, the constness of an iterator can be safely removed using:
foo::const_iterator cit = ... ;
foo::iterator it = c.erase( cit, cit );
However, this doesn't seem to work for forward_list
's equivalent, erase_after
, as per this code::
#include <iostream>
#include <forward_list>
typedef std::forward_list<int>::const_iterator CIT;
typedef std::forward_list<int>::iterator IT;
int main()
{
std::forward_list<int> m{1, 2, 3};
CIT cit = m.begin();
IT it = m.erase_after(cit, cit); // Segmentation fault!
std::cout << *it;
}
So is there any way to remove the constness of a const iterator for this class? Preferably not by iteration!