Quantcast
Channel: Recent Questions - Stack Overflow
Viewing all articles
Browse latest Browse all 12111

How would I refactor a method to erase an element based on a reverse iterator to avoid triggering a warning about copying a singular iterator?

$
0
0

I have a method that takes a stl reverse iterator, does some work, erases the element, and returns an iterator pointing to the next element. For example:

#include <vector>std::vector<int> int_vector;using ReverseIteratorType = decltype(int_vector)::reverse_iterator;void do_stuff();ReverseIteratorType erase_with_reverse_iter(ReverseIteratorType iter){    do_stuff();    std::advance(iter, 1);    int_vector.erase(iter.base());    return iter;}

This compiles without warnings and appears to work correctly. The problem is that when running this code with libstdc++ that verifies every iterator is valid when it is used, an exception is thrown if iter points to the last element of a collection because the iterator is treated as a singular iterator after erase, and calling the copy constructor of a singular iterator is disallowed. (There is a deeper explanation of the issue from another post here.).

godbolt example demonstrating the issue here

Two questions

  1. Is the disallowing of the copying of a singular iterator a violation of the standard, or is this a sanity check by libstdc++?
  2. How would you refactor this to avoid the issue?

Viewing all articles
Browse latest Browse all 12111

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>