c++ - gcc reverse_iterator comparison operators missing? -
I have a problem using Constant reverse iterators on non -content containers with GCC. OK, only some versions of GCC
#include & lt; Vector & gt; # Include & lt; Iostream & gt; using namespace std; Int main () {const char v0 [4] = "abc"; Of vector & lt; Four & gt; V (v0, v0 + 3); // this block ok vector & lt; Char & gt; :: const_iterator i; For (i = v.begin (); i! = V.end (); ++ i) COAT & LT; & Lt; * I; Cout & lt; & Lt; Endl; // This block is GCC 3.4.4 and GCC 4.0.1 vector & lt; Char & gt; :: Compile error with const_reverse_iterator r; For (r = v.rbegin (); r! = V.rend (); ++ r) COAT & LT; & Lt; * R; Cout & lt; & Lt; Endl; Return 0; }
This program compile properly and with GCC 4.2.1 (Mac Leopard) and with Visual Studio 8 and 9 (Windows) and with GCC 4.1.2 (Linux) Runs.
However, there is a compilation error with gcc 3.4.4 (Saigwin) and GCC 4.0.1 (Mac Snow Leopard).
test.cpp: 18: Error: 'There is no match for the operator! = 'In' R! = Std :: vector & lt; _Tp, _Alloc & gt; Code: Is this a bug in previous versions of gcc?
Due to other problems with GCC 4.2.1 on Mac, we need to use GCC 4.0.1 on Mac, so just using the new compiler is not the perfect solution for me, I suppose That is, I need to change how I use reverse Iterator. any suggestion?
This is a flaw in the current standard:
edit < / Strong>: One bit extension: The issue is that, in the current standard: -
vector :: reverse_iterator
was specified as std Is:: Reverse_iterator & lt; Vector :: iterator & gt;
, and vector :: const_reverse_iterator
as std :: reverse_iterator & lt; Vector :: const_iterator & gt;
. - On
std :: reverse_iterator
, the relational operators are defined with a template parameter, reverse_title and the reversibility & lt; Const_iterator & gt;
non-comparable.
In your code, you compare a const_reverse_iterator
with the result of calling "off ()" on a non-content vector, which is a ( Non -const) reverse_title
.
In C ++ 0x, two related changes are made to fix such issues:
- On the relational operator reverse_title, the rs now take two template parameters
- Containers like vector have explicit ways to request a const_iterator explicitly: cbegin (), cend (), crbegin () and crem ().
In your case, a solution must be explicitly requested const_reverse_iterator for: const:
vector & lt; Char & gt; :: const_reverse_iterator r; Construct Vector & lt; Char & gt; :: const_reverse_iterator crend = v.rend (); For (r = v. Arbijin (); r! = Crand; ++ r) COAT & lt; & Lt; * R;
Comments
Post a Comment