iter_swapping vector<bool>::iterator..

Chris Jefferson caj@cs.york.ac.uk
Fri Mar 18 16:22:00 GMT 2005


Paolo Carlini wrote:
> Anyone knows whether other implementors are able to provide a 
> vector<bool>::iterator
> which meets the forward iterator requirements? Everything really boils 
> down to this,
> in my opinion.

(Just thought I'd answer this bit.. but please answer if you have any
knowledge in this area, the whole iterator / vector<bool> / swap
business just confuses me..)

I don't think we can fix this with the implementation, looking at
23.2.5, the "reference" type has to be a class rather than a reference,
defined like:

// bit reference:
class reference {
   friend class vector;
   reference();
   public:
   ~reference();
   operator bool() const;
   reference& operator=(const bool x);
   reference& operator=(const reference& x);
   void flip(); // flips the bit
};


So it doesn't look like we can get around this... This would have been
fixed in TR1 with the extended iterator attributes, but they didn't seem
to get in in the end ¬_¬

While it won't fix anything if people are using their own proxy
iterators (although they shouldn't be, as the standard doesn't allow
them to), I don't imagine it would cause much pain to add:

void iter_swap(std::vector<bool>::iterator i,
                std::vector<bool>::iterator j)
{
   bool tmp = *i;
   *j = *i;
   *i = tmp;
}

(and perhaps even iter_swaps that take a std::vector<bool>::iterator and
a bool&?)

to namespace std if we want to preserve the old symantics (although of
course, you could then argue that we are adding a libstdc++-v3
extension, although we have been for quite a long time).

If this seems reasonable, I'll neaten it up / add a whole bunch of
testcases to make sure nothing is broken, etc :)

Chris



More information about the Libstdc++ mailing list