iter_swapping vector<bool>::iterator..
Felix Koehler
felix.koehler@gmail.com
Mon Mar 21 13:12:00 GMT 2005
I had just stumbled over this breaking my programs, when Chris
Jefferson reported the regression. Basically it makes vector<bool>
completely unusable as a lot of the standard algorithms like
"std::reverse" stop working.
Paolo Carline wrote:
>In my opinion, *if* we agree that vector<bool>::iterator cannot be
>implemented to meet the requirements of forward iterator, there is
>no point in adding these complications. Maybe just say something in
>the docs, nothing more: a standard conforming change of behavior is
>perfectly acceptable for a major release.
>Better spend our time elsewhere...
Well, for selfish reasons I obviously disagree ;-). vector<bool> with
most of the standard algorithms not working is a pain. Would
specialisations of swap / iter_swap help in the case of a derived
class T: public vector<bool> ? If so, please avoid breaking
vector<bool> for g++ 4.0.
Following testcase demonstrates how the breakage of iter_swap breaks
std::reverse for vector<bool> and derived clases. (Not a minimum
testcase, but one that should make sure that a fix will have no issues
with classes derived from vector<bool>).
----------------- <testcase start> -----------------------
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
class T: public std::vector<bool>
{
public:
T (int size = 0, bool value = false): std::vector < bool > (size,
value)
{
}
};
inline std::ostream&
operator << (std::ostream & o, const T & v)
{
for (int h = 0; h < v.size (); ++h)
o << (v[h] ? '1' : '0');
return o;
}
int main ()
{
T a(10, true);
cout << a << endl;
a[0] = false;
a[1] = false;
a[2] = false;
a[3] = false;
a[4] = false;
cout << a << endl;
reverse (a.begin(), a.end());
cout << a << endl;
return 0;
}
----------------- < testcase end> -----------------------
More information about the Libstdc++
mailing list