This is the mail archive of the
libstdc++@sourceware.cygnus.com
mailing list for the libstdc++ project.
problem with vector::assign
- To: libstdc++ at sourceware dot cygnus dot com
- Subject: problem with vector::assign
- From: scott snyder <snyder at fnal dot gov>
- Date: Fri, 10 Mar 2000 19:48:15 CST
hi -
I ran into a an instance in the std::vector implementation where
the code had not yet been adjusted for the change to use normal_iterator.
Trying to compile this code:
---------------------------------------------------------------
#include <vector>
using std::vector;
void foo (vector<int>& x)
{
vector<int> y;
y.assign (x.begin(), x.end());
}
---------------------------------------------------------------
gives me the errors:
inst/include/g++-v3/bits/stl_vector.h: In method `void std::vector<_Tp,
inst/include/g++-v3/bits/stl_vector.h: _Alloc>::_M_assign_aux
inst/include/g++-v3/bits/stl_vector.h: (_ForwardIter, _ForwardIter,
inst/include/g++-v3/bits/stl_vector.h: std::forward_iterator_tag) [with
inst/include/g++-v3/bits/stl_vector.h: _ForwardIterator =
inst/include/g++-v3/bits/stl_vector.h: std::__normal_iterator<int *,
inst/include/g++-v3/bits/stl_vector.h: std::vector<int,
inst/include/g++-v3/bits/stl_vector.h: std::allocator<int> > >, _Tp =
inst/include/g++-v3/bits/stl_vector.h: int, _Alloc =
inst/include/g++-v3/bits/stl_vector.h: std::allocator<int>]':
inst/include/g++-v3/bits/stl_vector.h:319: instantiated from here
inst/include/g++-v3/bits/stl_vector.h:612: cannot convert `std::__normal_iterator<int *,
inst/include/g++-v3/bits/stl_vector.h:612: std::vector<int, std::allocator<int> > >' to `int *' in
inst/include/g++-v3/bits/stl_vector.h:612: assignment
inst/include/g++-v3/bits/stl_vector.h:319: instantiated from here
inst/include/g++-v3/bits/stl_vector.h:618: cannot convert `std::__normal_iterator<int *,
inst/include/g++-v3/bits/stl_vector.h:618: std::vector<int, std::allocator<int> > >' to `int *' in
inst/include/g++-v3/bits/stl_vector.h:618: assignment
A patch is appended.
Upon inspecting the vector code, it looks like there is a similar
problem in the non-member template version of vector::insert.
Since we're not using that version, i didn't try to change it.
thanks,
sss
2000-03-10 scott snyder <snyder@fnal.gov>
* stl/bits/stl_vector.h (_M_assign_aux): Fix for __normal_iterator
conversions.
* testsuite/23_containers/vector_modifiers.cc (test01): Add a
regression test for the problem.
Index: stl/bits/stl_vector.h
===================================================================
RCS file: /cvs/libstdc++/libstdc++/stl/bits/stl_vector.h,v
retrieving revision 1.17
diff -u -p -r1.17 stl_vector.h
--- stl_vector.h 2000/03/01 02:29:13 1.17
+++ stl_vector.h 2000/03/11 00:49:13
@@ -606,7 +606,7 @@ vector<_Tp, _Alloc>::_M_assign_aux(_Forw
distance(__first, __last, __len);
if (__len > capacity()) {
- iterator __tmp(_M_allocate_and_copy(__len, __first, __last));
+ pointer __tmp(_M_allocate_and_copy(__len, __first, __last));
destroy(_M_start, _M_finish);
_M_deallocate(_M_start, _M_end_of_storage - _M_start);
_M_start = __tmp;
@@ -615,7 +615,7 @@ vector<_Tp, _Alloc>::_M_assign_aux(_Forw
else if (size() >= __len) {
iterator __new_finish(copy(__first, __last, _M_start));
destroy(__new_finish, end());
- _M_finish = __new_finish;
+ _M_finish = __new_finish.base();
}
else {
_ForwardIter __mid = __first;
Index: testsuite/23_containers/vector_modifiers.cc
===================================================================
RCS file: /cvs/libstdc++/libstdc++/testsuite/23_containers/vector_modifiers.cc,v
retrieving revision 1.1
diff -u -p -r1.1 vector_modifiers.cc
--- vector_modifiers.cc 1999/11/10 00:47:10 1.1
+++ vector_modifiers.cc 2000/03/11 00:49:30
@@ -49,6 +49,9 @@ bool test01()
A<B>* pnp01 = &np01;
vec02.insert(vec02.begin(), pnp01, pnp01 + 1);
+ // Test that assign compiles.
+ vec01.assign (pi01, pi01 + 1);
+
#ifdef DEBUG_ASSERT
assert(test);
#endif