how to define my allocator

P

papamms

hi!
i want to define my allocator in my allocator, so i can make a
memory pool for my stl vector list .....

code like this

#include <vector>
#include <list>

#include <iostream>

template <typename T>
class gs_allocator //: public allocator<T>
{
public:
typedef T value_type;
typedef value_type* pointer;
typedef const value_type* const_pointer;
typedef value_type& reference;
typedef const value_type& const_reference;
typedef std::size_t size_type;
typedef std::ptrdiff_t difference_type;

gs_allocator() {}
gs_allocator(const gs_allocator&) {}
~gs_allocator() {}

// rebind the class type u
template <class u>
struct rebind{
typedef gs_allocator<u> other;
};

pointer address(reference x) const { return &x; }
const_pointer address(const_reference x) const
{
return &x;
}

pointer allocate(size_type n, const_pointer = 0)
{
void* p = std::malloc(n * sizeof(T));
if (!p)
throw std::bad_alloc();

return static_cast<pointer>(p);
}

void deallocate(pointer p, size_type)
{
std::free(p);
}

size_type max_size() const
{
return static_cast<size_type>(-1) / sizeof(value_type);
//return size_type(UNIT_MAX/sizeof(T));
}

void construct(pointer p, const value_type& x)
{
new(p) value_type(x);
}

void destroy(pointer p) { p->~value_type(); }

private:
void operator = (const gs_allocator&);
};


template<typename _T1, typename _T2>
inline bool operator == (const gs_allocator<_T1>&, const
gs_allocator<_T2>&)
{
return true;
}

template<typename _T1, typename _T2>
inline bool operator != (const gs_allocator<_T1>&, const
gs_allocator<_T2>&)
{
return false;
}


int main()
{
typedef std::list<int, gs_allocator<int> >gslist;
gslist mylist;
mylist.push_back(1);
mylist.push_back(1);
mylist.push_back(1);



return 0;
}

vc 2005

Error 1 error C2664: 'gs_allocator<T>::gs_allocator(const
gs_allocator<T> &)' : cannot convert parameter 1 from
'gs_allocator<T>' to 'const gs_allocator<T> &' c:\program files
\microsoft visual studio 9.0\vc\include\list 67


i had defined gs_allocator(const gs_allocator&) {} , so what's
wrong?
 
P

Paul Bibbings

vc 2005

Error 1 error C2664: 'gs_allocator<T>::gs_allocator(const
gs_allocator<T> &)' : cannot convert parameter 1 from
'gs_allocator<T>' to 'const gs_allocator<T> &' c:\program files
\microsoft visual studio 9.0\vc\include\list 67


i had defined gs_allocator(const gs_allocator&) {} , so what's
wrong?

The error message from gcc (or Comeau) is more informative.

// ...
/opt/gcc-4.4.3/bin/../lib/gcc/i686-pc-cygwin/4.4.3/
include/c++/bits/stl_list.h:335:
error: no matching function for call to
¡®gs_allocator<int>::gs_allocator(const
gs_allocator<std::_List_node<int> >&)¡¯
custom_alloc.cpp:21: note: candidates are:
gs_allocator<T>::gs_allocator(const gs_allocator<T>&) [with T =
int]
custom_alloc.cpp:20: note:
gs_allocator<T>::gs_allocator() [with T = int]

This copy ctor evidently needs to be:

template<typename U>
gs_allocator(const gs_allocator<U>&) { }

Regards

Paul Bibbings
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,186
Members
46,740
Latest member
JudsonFrie

Latest Threads

Top