11

The following example will not compile using g++ 4.8.2:

#include <iostream>
#include <vector>
using namespace std;

int main() {
    vector<int> v {1, 2, 3};

    v.erase(v.cbegin()); // Compiler complains

    return 0;
}

The compiler says the following. (It isn't very readable, but it's complaining that there's not a known conversion between vector<int>::const_iterator and vector<int>::iterator.)

prog.cpp: In function ‘int main()’:
prog.cpp:8:20: error: no matching function for call to ‘std::vector<int>::erase(std::vector<int>::const_iterator)’
  v.erase(v.cbegin());
                    ^
prog.cpp:8:20: note: candidates are:
In file included from /usr/include/c++/4.8/vector:69:0,
                 from prog.cpp:2:
/usr/include/c++/4.8/bits/vector.tcc:134:5: note: std::vector<_Tp, _Alloc>::iterator std::vector<_Tp, _Alloc>::erase(std::vector<_Tp, _Alloc>::iterator) [with _Tp = int; _Alloc = std::allocator<int>; std::vector<_Tp, _Alloc>::iterator = __gnu_cxx::__normal_iterator<int*, std::vector<int> >; typename std::_Vector_base<_Tp, _Alloc>::pointer = int*]
     vector<_Tp, _Alloc>::
     ^
/usr/include/c++/4.8/bits/vector.tcc:134:5: note:   no known conversion for argument 1 from ‘std::vector<int>::const_iterator {aka __gnu_cxx::__normal_iterator<const int*, std::vector<int> >}’ to ‘std::vector<int>::iterator {aka __gnu_cxx::__normal_iterator<int*, std::vector<int> >}’
/usr/include/c++/4.8/bits/vector.tcc:146:5: note: std::vector<_Tp, _Alloc>::iterator std::vector<_Tp, _Alloc>::erase(std::vector<_Tp, _Alloc>::iterator, std::vector<_Tp, _Alloc>::iterator) [with _Tp = int; _Alloc = std::allocator<int>; std::vector<_Tp, _Alloc>::iterator = __gnu_cxx::__normal_iterator<int*, std::vector<int> >; typename std::_Vector_base<_Tp, _Alloc>::pointer = int*]
     vector<_Tp, _Alloc>::
     ^
/usr/include/c++/4.8/bits/vector.tcc:146:5: note:   candidate expects 2 arguments, 1 provided

Why? The C++11 standard plainly states, in §23.3.6.5, that the vector::erase function takes a const_iterator. (Paraphrases are here and here.)

What's a good workaround, assuming that I must use a const_iterator?

George Hilliard
  • 15,402
  • 9
  • 58
  • 96

2 Answers2

14

This is a known bug in gcc: http://gcc.gnu.org/bugzilla/show_bug.cgi?id=57158

erase requires an iterator instead of a const_iterator with current gcc's.

mirk
  • 5,302
  • 3
  • 32
  • 49
  • 1
    Hmmm. That bug is marked a duplicate of a resolved bug, but for `deque`. – George Hilliard Oct 24 '13 at 07:15
  • 2
    @thirtythreeforty "Target Milestone: 4.9.0" Resolved doesn't mean "fixed in every version". – stefan Oct 24 '13 at 07:17
  • @stefan Oh, gotcha, I missed that. Well drat. Any tips on converting between the two, or should I just suck it up and use a non-`const` iterator? – George Hilliard Oct 24 '13 at 07:18
  • 1
    @thirtythreeforty Seeing as you need a hack around a specific compiler & library version, you could look at their data layout and see if you could just `reinterpret_cast` through pointers (or use a `union { const_iterator ci; iterator i; }`). – Angew is no longer proud of SO Oct 24 '13 at 07:25
  • 1
    Just a note, I *can* use a `reinterpret_cast` (I tried it and **it works**), but **this is definitely a hack and not to be used if reliability/portability is required.** – George Hilliard Oct 27 '13 at 00:09
  • 1
    The `reinterpret_cast` is undefined behaviour, doing so violates the type-based alias analysis and could lead to strange results. – Jonathan Wakely Jul 11 '15 at 16:54
  • Technically that's the `list` version of the bug, the vector version is [55675](https://gcc.gnu.org/bugzilla/show_bug.cgi?id=55675) – Barry Jul 13 '15 at 14:48
8

You can get a non-const iterator via pointer arithmetic, here's a helper function to do that:

template<typename T>
  typename std::vector<T>::iterator
  const_iterator_cast(std::vector<T>& v, typename std::vector<T>::const_iterator iter)
  {
    return v.begin() + (iter - v.cbegin());
  }

used like so:

std::vector<T> v(1);
auto citer = v.cbegin();
v.erase( const_iterator_cast(v, citer) );
Barry
  • 286,269
  • 29
  • 621
  • 977
Jonathan Wakely
  • 166,810
  • 27
  • 341
  • 521
  • @rabensky: Try ```return std::next(v.begin(), std::distance(v.cbegin(), iter));```. – ManuelAtWork May 15 '17 at 08:59
  • @rabensky which is fine because the question is specifically about `std::vector` and that has random access iterators. And the helper function clearly takes an argument of type `std::vector&`, so it only works with `std::vector`. – Jonathan Wakely May 15 '17 at 10:29