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]

Re: Proposal for std::vector.


On Sat, 2004-02-07 at 00:26, Matt Austern wrote:
> On Feb 6, 2004, at 4:51 AM, Dhruv Matani wrote:
> 
> > The relevant discussion can be found by searching google for:
> > About std::vector and standard containers in general.
> >
> >
> > Wierd as it may seem, are standard containers such as std::vector
> > allowed
> > to derive from std::allocator, or more generally from an allocator
> > class?
> >
> > Mainly used to make use of EBO in case of an empty allocator such as
> > new_allocator and malloc_allocator.
> >
> >
> >
> > I liked a solution that one poster mentioned about having an IMPL class
> > within vector. So something like this:
> >
> >
> > template <T, A>
> > class vector {
> >    template <_A>
> >    struct IMPL : public _A {
> >       _M_start, _M_finish, etc...
> >    };
> > };
> >
> >
> > What do you think about it? Can it be merged with the library?
> 
> Right now, in our implementation, the standard containers *do*
> inherit from allocators.  I can't think of any reason why it would
> be prohibited.  It's only prohibited if it would cause some
> conforming program to fail, and I can't think of any.
> 
> Your IMPL technique would work too, but it's more complicated
> and I don't really see much benefit to justify the extra work.
> What do you see as the advantages?

I have here these 2 test programs. One of them tries to emulate the
vector (vtest.cpp), and the other one actually uses the vector. Now, the
emulation produces (IMHO) correct output, but the one that uses the
vector does not. I may be missing something somewhere, but I don't know
where! I've basically tried to introduce a virtual clear() in the
allocator and then call it in the deallocate function, but somehow the
Allocator's clear() is being called instead of the vector's clear?




-- 
	-Dhruv Matani.
http://www.geocities.com/dhruvbird/


#include <iostream>
using namespace std;


struct Alloc {
  virtual void clear () { cout<<"In Alloc::clear()"<<endl; }
  void allocate () { this->clear(); }
};


struct Vector : protected Alloc {
  void clear () { cout<<"In Vector::clear()"<<endl; }
  //  void allocate () { Alloc::allocate(); }
  void push_back () { this->allocate(); }
};


int main ()
{
  Vector *d = new Vector;
  d->push_back();
}
#include <iostream>
using namespace std;

#include <vector>
#include <ext/new_allocator.h>
using namespace std;
using __gnu_cxx::new_allocator;


template <typename T>
class Whacky_Alloc : public new_allocator<T> {
  static int Count;
  Whacky_Alloc& operator= (Whacky_Alloc const&);
public:

  virtual void clear ()
  {
    cout<<"In Whacky_Alloc::clear()"<<endl;
  }
  Whacky_Alloc () { cout<<"In ctor"<<endl; ++Count; }
  Whacky_Alloc (Whacky_Alloc const& _wa) { ++Count; }
  virtual ~Whacky_Alloc ()
  {
    --Count;
    cout<<"Calling My Clear -> Next Line Should be Whacky_Alloc::clear()"<<endl;
    this->clear();
    if (!Count)
      cout<<"All Clear"<<endl;
  }

  T *allocate (typename new_allocator<T>::size_type n, const void *hint = 0)
  { return new_allocator<T>::allocate(n, hint); }
  void deallocate (T *ptr, typename new_allocator<T>::size_type n)
  {
    cout<<"Call Clear Now!"<<endl;
    this->clear();
    new_allocator<T>::deallocate (ptr, n);
  }

};

template<typename X>
int Whacky_Alloc<X>::Count = 0;


int main()
{
  vector<int, Whacky_Alloc<int> > *piv = new vector<int, Whacky_Alloc<int> >;
  //vector<int> iv;
  piv->push_back (23);
  //  iv.clear ();
  delete piv;

  //  Whacky_Alloc<int> iwa;
}

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