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:trdiff_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?
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:trdiff_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?