operator != for const_reverse_iterator

S

Spoon

Hello everyone,

Could someone please confirm that the following program is valid:

#include <list>
typedef std::list<int> List;
int main()
{
List foo;
foo.push_back(1);
foo.push_back(2);
int accu = 0;
List::const_reverse_iterator it;
for (it = foo.rbegin(); it != foo.rend(); ++it) accu += *it;
return accu;
}

$ g++ -std=c++98 -Wall -Wextra mini.cxx
mini.cxx: In function `int main()':
mini.cxx:10: error: no match for 'operator!=' in 'it != std::list<_Tp,
_Alloc>::rend() [with _Tp = int, _Alloc = std::allocator<int>]()'

AFAIU, this is a compiler bug.
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=11729

I suppose my version of GCC is starting to show its age.
(3.4.4 was released on 2005-05-18.)
However 3.4.6 (released on 2006-03-06) produces the same error.

I suppose that the appropriate work around is to change
const_reverse_iterator to reverse_iterator?

Regards.
 
I

Ivan Vecerina

: Hello everyone,
:
: Could someone please confirm that the following program is valid:
:
: #include <list>
: typedef std::list<int> List;
: int main()
: {
: List foo;
: foo.push_back(1);
: foo.push_back(2);
: int accu = 0;
: List::const_reverse_iterator it;
: for (it = foo.rbegin(); it != foo.rend(); ++it) accu += *it;
: return accu;
: }
:
: $ g++ -std=c++98 -Wall -Wextra mini.cxx
: mini.cxx: In function `int main()':
: mini.cxx:10: error: no match for 'operator!=' in 'it != std::list<_Tp,
: _Alloc>::rend() [with _Tp = int, _Alloc = std::allocator<int>]()'
:
: AFAIU, this is a compiler bug.
: http://gcc.gnu.org/bugzilla/show_bug.cgi?id=11729

I am not sure if the standard specifies that operators must
allow comparison between const_ and non-const iterators.
The code does compile in VS2005, though not with Comeau.

In any case, it is more likely to be a bug in the Library,
rather than in the compiler.

: I suppose my version of GCC is starting to show its age.
: (3.4.4 was released on 2005-05-18.)
: However 3.4.6 (released on 2006-03-06) produces the same error.
:
: I suppose that the appropriate work around is to change
: const_reverse_iterator to reverse_iterator?
This will do.
Or you can use a const reference to the list, or a utility
function such as:
template<typename T> T const& cst(T& p) { return p; }
leading to:
for (it = foo.rbegin(); it != cst(foo).rend(); ++it)

Note that the standard library containers in C++0x are expected
to include new member functions: cbegin(), crend() etc that
always return const_ versions of the iterators.


hth -Ivan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top