This is the mail archive of the libstdc++@sourceware.cygnus.com 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]

problems with vector::iterator: 2



hi -

Here's a second problem with std::vector iterators.
These tests and changes are based
on the september snapshot (2.90.6.1), but from an inspection of the
change logs, it doesn't look like there have been any relevant changes
since then.

If i try to compile the following program using gcc 2.95.2 (on
a i686-pc-linux-gnu platform) and libstdc++:

----------------------------------------------------
#include <vector>

void foo (std::vector<int>& v, int* p)
{
  v.insert (v.begin(), p, p+1);
}
----------------------------------------------------

I get the errors:

$ g++ -c -I/usr/local/libstdc++/include/g++-v3 test2.cc
/usr/local/libstdc++/include/g++-v3/bits/stl_vector.h: In method `void vector<int,allocator<int> >::_M_range_insert<int *>(__normal_iterator<int *,vector<int,allocator<int> > >, int *, int *, forward_iterator_tag)':
/usr/local/libstdc++/include/g++-v3/bits/stl_vector.h:393:   instantiated from here
/usr/local/libstdc++/include/g++-v3/bits/stl_vector.h:767: no match for `int *& - __normal_iterator<int *,vector<int,allocator<int> > > &'
/usr/local/libstdc++/include/g++-v3/bits/stl_vector.h:393:   instantiated from here
/usr/local/libstdc++/include/g++-v3/bits/stl_vector.h:791: no matching function for call to `uninitialized_copy (int *&, __normal_iterator<int *,vector<int,allocator<int> > > &, __normal_iterator<int *,vector<int,allocator<int> > > &)'
/usr/local/libstdc++/include/g++-v3/bits/stl_uninitialized.h:86: candidates are: char * uninitialized_copy(const char *, const char *, char *)
/usr/local/libstdc++/include/g++-v3/bits/stl_uninitialized.h:94:                 __wchar_t * uninitialized_copy(const __wchar_t *, const __wchar_t *, __wchar_t *)
/usr/local/libstdc++/include/g++-v3/bits/stl_vector.h:393:   instantiated from here
/usr/local/libstdc++/include/g++-v3/bits/stl_vector.h:794: no matching function for call to `uninitialized_copy (__normal_iterator<int *,vector<int,allocator<int> > > &, int *&, __normal_iterator<int *,vector<int,allocator<int> > > &)'
/usr/local/libstdc++/include/g++-v3/bits/stl_uninitialized.h:86: candidates are: char * uninitialized_copy(const char *, const char *, char *)
/usr/local/libstdc++/include/g++-v3/bits/stl_uninitialized.h:94:                 __wchar_t * uninitialized_copy(const __wchar_t *, const __wchar_t *, __wchar_t *)
/usr/local/libstdc++/include/g++-v3/bits/stl_vector.h:393:   instantiated from here
/usr/local/libstdc++/include/g++-v3/bits/stl_vector.h:797: no matching function for call to `vector<int,allocator<int> >::_M_deallocate (__normal_iterator<int *,vector<int,allocator<int> > > &, const size_t &)'
/usr/local/libstdc++/include/g++-v3/bits/stl_vector.h:95: candidates are: void _Vector_alloc_base<int,allocator<int>,>::_M_deallocate(int *, unsigned int)
/usr/local/libstdc++/include/g++-v3/bits/stl_vector.h:393:   instantiated from here
/usr/local/libstdc++/include/g++-v3/bits/stl_vector.h:800: cannot convert `__new_start' from type `__normal_iterator<int *,vector<int,allocator<int> > >' to type `int *'
/usr/local/libstdc++/include/g++-v3/bits/stl_vector.h:801: cannot convert `__new_finish' from type `__normal_iterator<int *,vector<int,allocator<int> > >' to type `int *'
/usr/local/libstdc++/include/g++-v3/bits/stl_vector.h:393:   instantiated from here
/usr/local/libstdc++/include/g++-v3/bits/stl_vector.h:802: cannot convert `__normal_iterator<int *,vector<int,allocator<int> > >((&__len))' from type `__normal_iterator<int *,vector<int,allocator<int> > >' to type `int *'


These errors seem to stem from mixing together the vector iterators
and _M_start, etc., which are pointers.

The following change seems to fix it:

1999-11-04  scott snyder <snyder@fnal.gov>

	* stl_vector.h (_M_range_insert): Fix mixing pointers and
	vector::iterator.


--- /usr/local/libstdc++/include/g++-v3/bits/stl_vector.h	Sun Oct 24 21:22:52 1999
+++ stl_vector.h	Thu Nov  4 21:47:58 1999
@@ -764,7 +764,7 @@
     size_type __n = 0;
     distance(__first, __last, __n);
     if (size_type(_M_end_of_storage - _M_finish) >= __n) {
-      const size_type __elems_after = _M_finish - __position;
+      const size_type __elems_after = _M_finish - __position.base();
       iterator __old_finish(_M_finish);
       if (__elems_after > __n) {
         uninitialized_copy(_M_finish - __n, _M_finish, _M_finish);
@@ -788,18 +788,19 @@
       iterator __new_start(_M_allocate(__len));
       iterator __new_finish(__new_start);
       __STL_TRY {
-        __new_finish = uninitialized_copy(_M_start, __position, __new_start);
+        __new_finish = uninitialized_copy(iterator (_M_start), __position,
+                                          __new_start);
         __new_finish = uninitialized_copy(__first, __last, __new_finish);
         __new_finish
-          = uninitialized_copy(__position, _M_finish, __new_finish);
+          = uninitialized_copy(__position, iterator (_M_finish), __new_finish);
       }
       __STL_UNWIND((destroy(__new_start,__new_finish), 
-                    _M_deallocate(__new_start,__len)));
+                    _M_deallocate(__new_start.base(),__len)));
       destroy(_M_start, _M_finish);
       _M_deallocate(_M_start, _M_end_of_storage - _M_start);
-      _M_start = __new_start;
-      _M_finish = __new_finish;
-      _M_end_of_storage = __new_start + __len;
+      _M_start = __new_start.base();
+      _M_finish = __new_finish.base();
+      _M_end_of_storage = __new_start.base() + __len;
     }
   }
 }



thanks,
sss

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