This is the mail archive of the libstdc++@gcc.gnu.org mailing list for the libstdc++ project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[PATCH] Fix libstdc++/6642


Hi,

the following simple patch would close the PR by implementing the 
resolution of DR179 <Ready>, which prescribes that not only the various 
comparison operators but also operator- must accept mixed 
iterator/const_iterator parameters.
There a few issues, however, about which I'm not sure:
1- First and foremost the obnoxious ABI issue: this case seems more 
delicate than 6503, but I have verified that, for instance, a testcase 
like the following:

#include <string>

class MyClass {
private:
  std::string s;
  std::string::iterator it;
public:
  std::string::size_type pos() const {
    return s.begin() - it;
  }
};

int main()
{
  MyClass cl;
  cl.pos();
}

can be both compiled with 3.1 and linked with trunk+my patch and 
viceversa. But more tests *and* general guidelines are needed...

2- I didn't wrap operator- in #ifdef _GLIBCPP_RESOLVE_LIB_DEFECTS since, 
otherwise, all the other non-member comparison operators should be 
wrapped too and provided also in the member form #ifndef. More 
generally, I seem to remember some mailings from Gaby maintaining that 
in fact the <Ready> DR are in fact part of the standard for all 
practical purposes.

3- As regards the testcase, I believe it belongs to 
24_iterators/iterator.cc, but I noticed only today that the whole file 
has an highly non-standard structure, which I tried to follow to my best.

4- Finally, a similar fix should be probably prepared also for the 
special _Deque_iterator type, but in that case seems more difficult to 
limit it to non-reverse iterators. The corresponding DR for the latter 
is #280 which is still in the <Open> state.

Tested i686-pc-linux-gnu.

Ciao,
Paolo.

////////////////

2002-05-26  Paolo Carlini  <pcarlini@unitus.it>

        PR libstdc++/6642
        * include/bits/stl_iterator.h
        (__normal_iterator::operator-(const __normal_iterator&)):
        Make non-member, as already happens for the comparison
        operators in accord with DR179 <Ready>.
        * testsuite/24_iterators/iterator.cc: Add test from the PR.

diff -urN libstdc++-v3-orig/include/bits/stl_iterator.h 
libstdc++-v3/include/bits/stl_iterator.h
--- libstdc++-v3-orig/include/bits/stl_iterator.h    2002-04-16 
04:29:20.000000000 +0200
+++ libstdc++-v3/include/bits/stl_iterator.h    2002-05-26 
19:02:12.000000000 +0200
@@ -629,10 +629,6 @@
       operator-(const difference_type& __n) const
       { return __normal_iterator(_M_current - __n); }
      
-      difference_type
-      operator-(const __normal_iterator& __i) const
-      { return _M_current - __i._M_current; }
-     
       const _Iterator&
       base() const { return _M_current; }
     };
@@ -719,6 +715,12 @@
          const __normal_iterator<_Iterator, _Container>& __rhs)
   { return __lhs.base() >= __rhs.base(); }
 
+  template<typename _IteratorL, typename _IteratorR, typename _Container>
+  inline typename __normal_iterator<_IteratorL, 
_Container>::difference_type
+  operator-(const __normal_iterator<_IteratorL, _Container>& __lhs,
+         const __normal_iterator<_IteratorR, _Container>& __rhs)
+  { return __lhs.base() - __rhs.base(); }
+
   template<typename _Iterator, typename _Container>
   inline __normal_iterator<_Iterator, _Container>
   operator+(typename __normal_iterator<_Iterator, 
_Container>::difference_type __n,
diff -urN libstdc++-v3-orig/testsuite/24_iterators/iterator.cc 
libstdc++-v3/testsuite/24_iterators/iterator.cc
--- libstdc++-v3-orig/testsuite/24_iterators/iterator.cc    2001-11-23 
17:29:01.000000000 +0100
+++ libstdc++-v3/testsuite/24_iterators/iterator.cc    2002-05-26 
19:32:38.000000000 +0200
@@ -577,6 +577,17 @@
    return failures;
 }
 
+// libstdc++/6642
+int
+test6642()
+{
+   std::string s;
+   std::string::iterator it = s.begin();
+   std::string::const_iterator cit = s.begin();
+ 
+   return it - cit;
+}
+
 int
 main(int argc, char **argv)
 {
@@ -590,6 +601,8 @@
 
    failures += wrong_stuff();
 
+   failures += test6642();
+
 #ifdef DEBUG_ASSERT
    assert (failures == 0);
 #endif


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]