Hello gurus!
My goal is to write a STL compliant container customed fit to my personal need.
For starters, I'm practically duplicating std::vector to model the container's basic functionalities.
As for the memory manager, I decided to use std::allocator to make the container more typesafe, but unfortunately that is where I ran into problems.
The problem is flagged by the Comeau Online as an error, and first warning and later also as error by VC++ 2008, but the minGW port of g++ does not complain at all (compiles ok)
here is what I think I'm doing with the allocator class as below
Actual implementation of the code snippet above is not that much complex either, which isCode:template <typename Type> struct Foo { typedef std::allocator<Type> Alloc; Foo(unsigned int size) : capacity(size), start(0), alloc(Alloc()) { start = alloc.allocate(size); } ~Foo() { alloc.destroy(start); alloc.deallocate(start, capacity); } Foo& push(const Type& val) { alloc.construct(start, val); // line in question return *this; } private: unsigned int capacity; Type* start; Alloc alloc; };
Where Allocator_Type_ is passed as std::allocator inCode://! Container base class template <typename Tp_, typename Alloc_> struct Container_Base_ { typedef Tp_ Value_Type_; typedef Value_Type_* Pointer_Type_; typedef Alloc_ Allocator_Type_; typedef unsigned int Size_Type_; typedef std::ptrdiff_t Difference_Type_; struct Alloc_Impl_ : public Allocator_Type_ { explicit Alloc_Impl_(const Allocator_Type_& alloc) : Allocator_Type_(alloc) , M_Start_(0) , M_Next_(0) , M_Elements_(0) { } Value_Type_* M_Start_; Value_Type_* M_Next_; Value_Type_* M_Elements_; }; // ... };
and the error picked up by Comeau lies inCode://!Lockable Vector template <typename Tp_, typename Alloc_ = std::template allocator<Tp_> > class Vector : private Spud::Container_Base_<Tp_, Alloc_>;
And the error message isCode:Vector_Type_& push_back(const_reference t) { if(M_Impl_.M_Next_ == M_Impl_.M_Elements_) M_Impl_Reallocate_(); // Comeau & MSVC flag this as an error M_Impl_.construct(M_Impl_.M_Next_, t); ++M_Impl_.M_Next_; return *this; }
My understanding of the std::allocator was that I don't need to directly deal with operator new library function, but seeing the error message I'm confused, or simply I'm totally not getting something right.Originally Posted by Comeau C/C++ 4.3.10.1 (Oct 6 2008 11:28:09) for ONLINE_EVALUATION_BETA2
I'd appreciate it if anyone could help me figure it out.
Thank you.




ush_back(const Tp_ &) [with Tp_=int,
Reply With Quote