8

I believe that since C++11, the erase function of most containers (e.g. std::vector) accepts a const_iterator as parameter:

iterator erase (const_iterator position);

Still my compilers (GCC 4.8 and Clang 3.2, both using GCC libstdc++) won't allow me to use such function, even when compiling with --std=c++11.

Is it a compiler/libstdc++ bug, or did I do something bad? This is a sample code:

#include <vector>

int main( )
{
    std::vector<int> v;
    v.push_back( 1 );
    v.push_back( 2 );
    v.push_back( 3 );

    std::vector<int>::const_iterator i = v.begin();
    while( i != v.end() ) {
        i = v.erase( i );
    }

    return 0;
}
peoro
  • 25,562
  • 20
  • 98
  • 150
  • 3
    This seems to be an issue with the implementation of the Standard Library, not with the compiler itself – Andy Prowl Apr 13 '13 at 12:42
  • @Andy: yes, if the fault is not mine, it's most probably of libstdc++. Still I'm surprised that it's a problem of libstdc++ (or of the compiler, in any case), since I could find many questions about erasing a `const_iterator`, but **all** of them predate C++11... – peoro Apr 13 '13 at 12:48

1 Answers1

7

This issue is documented here and it's reported as a partial implementation for now.

CTRL + F with your browser and search for N2350.

If you are on Linux it's possible to build a development version of the libcxx library from the LLVM project that you can download from here; I don't know if this solves any of the issues that you are experiencing but I'm proposing it as an alternative to the libstdc++.

David G
  • 94,763
  • 41
  • 167
  • 253
user2244984
  • 449
  • 4
  • 11
  • 1
    You're right, I missed it. It's quite weird, though, that there are many questions about this function (on SO and all around the web), but none of them addresses its lack in `libstdc++`... – peoro Apr 14 '13 at 22:37
  • Has this been fixed by now? (June 2014?) – Jonathan Jun 13 '14 at 20:06
  • @JonathanLeaders: I just saw your message. Yes, now it's fixed. – peoro Jun 13 '16 at 23:43