This is the mail archive of the
libstdc++@sourceware.cygnus.com
mailing list for the libstdc++ project.
Bug in stl_vector.h?
- To: libstdc++@sourceware.cygnus.com
- Subject: Bug in stl_vector.h?
- From: "Andreas Gruenbacher (Diplomand MJ)" <agruenba@pent224.infosys.tuwien.ac.at>
- Date: Sat, 12 Jun 1999 07:50:05 +0200 (CEST)
Hello,
I have a very simple test case that fails to compile.
Versions:
gcc version gcc-2.95 19990608 (prerelease)
libstdc++-2.90.5
The program copies one vector over another. From EGCS I get:
----------------------------------------------------
> g++ -nostdinc++ -L/usr/local/lib -I/usr/local/include/g++-v3 -o vector_bug vector_bug.cc
/usr/local/include/g++-v3/bits/stl_vector.h: In method `class
vector<Character,allocator<Character> > &
vector<Character,allocator<Character> >::operator =(const
vector<Character,allocator<Character> > &)':
vector_bug.cc:44: instantiated from here
/usr/local/include/g++-v3/bits/stl_vector.h:559: no matching function
for call to `destroy (__normal_iterator<Character
*,vector<Character,allocator<Character> > > &, Character *&)'
/usr/local/include/g++-v3/bits/stl_construct.h:81: candidates are: void
destroy(char *, char *)
/usr/local/include/g++-v3/bits/stl_construct.h:82: void
destroy(__wchar_t *, __wchar_t *)
----------------------------------------------------
The program is as follows:
----------------------------------------------------
#include <iostream>
#include <vector>
class Character
{
char c;
bool copy;
public:
Character(char c)
: c(c), copy(false)
{
cerr << "Character::D('" << c << "')" << endl;
}
Character(const Character& d)
: c(d.c), copy(true)
{
cerr << "Character::D(" << c << ")" << endl;
}
~Character()
{
if (copy)
cerr <<"Character::~D(" << c << ")" << endl;
else
cerr << "Character::~Character('" << c << "')" << endl;
}
};
int
main(int argc, char*
argv[])
{
cerr << "---" << endl;
vector<Character> v1;
v1.push_back(Character('a'));
v1.push_back(Character('b'));
vector<Character> v2;
v2.push_back(Character('c'));
v2 = v1; // copy a vector -- without the
// patch this fails.
cerr << "---" << endl;
}
----------------------------------------------------
With the following path applied the program works:
----------------------------------------------------
--- stl_vector-buggy.h Sat Jun 12 07:26:44 1999
+++ stl_vector-repaired.h Sat Jun 12 07:26:24 1999
@@ -556,7 +556,7 @@ vector<_Tp,_Alloc>::operator=(const vect
}
else if (size() >= __xlen) {
iterator __i(copy(__x.begin(), __x.end(), begin()));
- destroy(__i, _M_finish);
+ destroy(__i, end());
}
else {
copy(__x.begin(), __x.begin() + size(), _M_start);
@@ -612,7 +612,7 @@ vector<_Tp, _Alloc>::_M_assign_aux(_Forw
}
else if (size() >= __len) {
iterator __new_finish(copy(__first, __last, _M_start));
- destroy(__new_finish, _M_finish);
+ destroy(__new_finish, end());
_M_finish = __new_finish;
}
else {
----------------------------------------------------
Andreas
-----------------------------------------------------------------------
Andreas Gruenbacher, Vienna University of Technology
a.gruenbacher@infosys.tuwien.ac.at
Contact information: http://www.infosys.tuwien.ac.at/~agruenba