Re: operator != for const_reverse_iterator
"Spoon" <devnull@localhost.com> wrote in message
news:4603993f$0$18244$426a74cc@news.free.fr...
: 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
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Brainbench MVP for C++ <> http://www.brainbench.com