How to obtain a list::iterator from a list::const_iterator

112 views Asked by At

If I have a (non-const) reference to a stl::list and a const_iterator to a particular location in that list, how can I obtain an iterator pointing to the same place? (I want to erase the element at that position, but list doesn't have an erase method for const_iterators)

This seems like it shouldn't violate const-correctness, since it only works if I have a non-const reference to the list.

EDIT:

Hold on, looking at the CPP reference: http://www.cplusplus.com/reference/list/list/erase/ it says you can call erase() with a const_iterator. So why is my compiler complaining? I'm using gcc 4.6 --std=c++0x

The error is:

g++ -D__GXX_EXPERIMENTAL_CXX0X__ -O0 -g3 -Wall --std=c++0x -c -fmessage-length=0 -MMD -MP -MF"src/constraint.d" -MT"src/constraint.d" -o "src/constraint.o" "../src/constraint.cpp"
../src/constraint.cpp: In member function ‘void constraint::remove_lit(std::list<std::pair<literal, int> >::const_iterator)’:
../src/constraint.cpp:144:18: error: no matching function for call to ‘std::list<std::pair<literal, int> >::erase(std::list<std::pair<literal, int> >::const_iterator&)’
../src/constraint.cpp:144:18: note: candidates are:
/usr/include/c++/4.6/bits/list.tcc:109:5: note: std::list<_Tp, _Alloc>::iterator std::list<_Tp, _Alloc>::erase(std::list<_Tp, _Alloc>::iterator) [with _Tp = std::pair<literal, int>, _Alloc = std::allocator<std::pair<literal, int> >, std::list<_Tp, _Alloc>::iterator = std::_List_iterator<std::pair<literal, int> >]
/usr/include/c++/4.6/bits/list.tcc:109:5: note:   no known conversion for argument 1 from ‘std::list<std::pair<literal, int> >::const_iterator {aka std::_List_const_iterator<std::pair<literal, int> >}’ to ‘std::_List_iterator<std::pair<literal, int> >’
/usr/include/c++/4.6/bits/stl_list.h:1160:7: note: std::list<_Tp, _Alloc>::iterator std::list<_Tp, _Alloc>::erase(std::list<_Tp, _Alloc>::iterator, std::list<_Tp, _Alloc>::iterator) [with _Tp = std::pair<literal, int>, _Alloc = std::allocator<std::pair<literal, int> >, std::list<_Tp, _Alloc>::iterator = std::_List_iterator<std::pair<literal, int> >]
/usr/include/c++/4.6/bits/stl_list.h:1160:7: note:   candidate expects 2 arguments, 1 provided
make: *** [src/constraint.o] Error 1
0

There are 0 answers