<vector>, const_reverse_iterator and rend()

Niall Smart njs3@doc.ic.ac.uk
Sat May 30 10:24:00 GMT 1998


Hi,

I intend the following code to initialise a vector with three
elements and print its contents in reverse order, but it does
not compile when BREAKME is defined:


#include <iostream>
#include <vector>

int
main()
{
        vector<int>             x(3);

        x[0] = 23;
        x[1] = 56;
        x[2] = 222;

        #ifdef BREAKME

        vector<int>& y = x;

        #else

        const vector<int>& y = x;

        #endif

        vector<int>::const_reverse_iterator i = y.rbegin();

        while (i != y.rend()) {
                cerr << *i << endl;
                ++i;
        }
}

[njs3@cnet0000 ~]$ g++ bug.cc -Wall -DBREAKME
/homes/njs3/pub/cc/egcs-19980525/include/g++/stl_iterator.h: In method
`reverse_iterator<const int *>::reverse_iterator<int *>(const class
reverse_iterator<int *> &)':
bug-stl.cc:23:   instantiated from here
/homes/njs3/pub/cc/egcs-19980525/include/g++/stl_iterator.h:548: member
`current' is a protected member of class `reverse_iterator<int *>'
bug-stl.cc: In function `int main()':
bug-stl.cc:25: no match for `reverse_iterator<const int *> & !=
reverse_iterator<int *>'

"y.rend()" and not "y.rend() const" seems to be getting called (for
-DBREAKME), and const_reverse_iterator and reverse_iterator do not seem
to be comparable,  is the problem with the compiler choosing the wrong
version of y.rend() or with the STL implementation?

Niall



More information about the Gcc-bugs mailing list