By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,804 Members | 1,666 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,804 IT Pros & Developers. It's quick & easy.

operator != for const_reverse_iterator

P: n/a
Hello everyone,

Could someone please confirm that the following program is valid:

#include <list>
typedef std::list<intList;
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.
Mar 23 '07 #1
Share this Question
Share on Google+
1 Reply


P: n/a
"Spoon" <de*****@localhost.comwrote in message
news:46***********************@news.free.fr...
: Hello everyone,
:
: Could someone please confirm that the following program is valid:
:
: #include <list>
: typedef std::list<intList;
: 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 TT 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

Mar 23 '07 #2

This discussion thread is closed

Replies have been disabled for this discussion.