Missing const_iterator overload of std::vector::erase() with g++ 4.8

匿名 (未验证) 提交于 2019-12-03 01:58:03

问题:

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

#include  #include  using namespace std;  int main() {     vector 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::const_iterator and vector::iterator.)

prog.cpp: In function int main()’: prog.cpp:8:20: error: no matching function for call to std::vector::erase(std::vector::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; std::vector<_tp _alloc="">::iterator = __gnu_cxx::__normal_iterator >; 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::const_iterator {aka __gnu_cxx::__normal_iterator >}’ to std::vector::iterator {aka __gnu_cxx::__normal_iterator >}’ /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; std::vector<_tp _alloc="">::iterator = __gnu_cxx::__normal_iterator >; 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?

回答1:

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.



回答2:

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

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

used like so:

std::vector v(1); auto citer = v.cbegin(); v.erase( const_iterator_cast(v, citer) );


易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!