0

I have written a custom allocator that allocates memory in a shared memory segment.

This line of code compiles (and runs) fine:

 shp_arr = new (vecmem) vector<shape *,smallocator <shape*> > ;

But this line of code:

shp_queue = new (queuemem) queue< shape *, deque < shape *, smallocator< shape * > > > ;

gives me a number of errors. Here they are:

/usr/local/lib/gcc/i686-pc-cygwin/4.7.3/../../../../include/c++/4.7.3/bits/stl_deque.h: In 
instantiation of ‘std::_Deque_base<_Tp, _Alloc>::_Map_alloc_type std::_Deque_base<_Tp, 
_Alloc>::_M_get_map_allocator() const [with _Tp = shape*; _Alloc = smallocator<shape*>;
std::_Deque_base<_Tp, _Alloc>::_Map_alloc_type = smallocator<shape**>]’:
/usr/local/lib/gcc/i686-pc-cygwin/4.7.3/../../../../include/c++/4.7.3/bits/stl_deque.h:549:9:   
required from ‘void std::_Deque_base<_Tp, _Alloc>::_M_deallocate_map(_Tp**, std::size_t) [with _Tp = 
shape*; _Alloc = smallocator<shape*>; std::size_t = unsigned int]’
/usr/local/lib/gcc/i686-pc-cygwin/4.7.3/../../../../include/c++/4.7.3/bits/stl_deque.h:568:4: 
required from ‘std::_Deque_base<_Tp, _Alloc>::~_Deque_base() [with _Tp = shape*; _Alloc = 
smallocator<shape*>]’
/usr/local/lib/gcc/i686-pc-cygwin/4.7.3/../../../../include/c++/4.7.3/bits/stl_deque.h:781:15:     
required from ‘std::deque<_Tp, _Alloc>::deque() [with _Tp = shape*; _Alloc = smallocator<shape*>]’
file.cpp:233:30:   required from here
/usr/local/lib/gcc/i686-pc-cygwin/4.7.3/../../../../include/c++/4.7.3/bits/stl_deque.h:529:53: error:  
no matching function for call to ‘smallocator<shape**>::smallocator(const _Tp_alloc_type&)’
/usr/local/lib/gcc/i686-pc-cygwin/4.7.3/../../../../include/c++/4.7.3/bits/stl_deque.h:529:53: note: 
candidates are:
In file included from file.cpp:20:0:
smallocator.hpp:41:3: note: smallocator<T>::smallocator(const smallocator<T>&) [with T = shape**; 
smallocator<T> = smallocator<shape**>]
smallocator.hpp:41:3: note:   no known conversion for argument 1 from ‘const _Tp_alloc_type {aka const 
smallocator<shape*>}’ to ‘const smallocator<shape**>&’
smallocator.hpp:40:3: note: smallocator<T>::smallocator() [with T = shape**]
smallocator.hpp:40:3: note:   candidate expects 0 arguments, 1 provided

The interface to smallocator looks like this:

template <typename T>
class smallocator: public std::allocator<T>
  {
    public:
      typedef size_t size_type;
      typedef T* pointer;
      typedef const T* const_pointer;

      template<typename _Tp1>
      struct rebind
        {
          typedef smallocator<_Tp1> other;
        };

      pointer allocate(size_type n, const void *hint=0)
        {
         ... 
        }

      void deallocate(pointer p, size_type n)
        {
         ...
        }

      smallocator() throw(): std::allocator<T>() { std::cout <<"Hello allocator" <<std::endl;}
      smallocator(const smallocator &a) throw(): std::allocator<T>(a) { }
      ~smallocator() throw() { }  
  };

Does anyone know what the problem is? Thanks!

user2358643
  • 25
  • 1
  • 6

1 Answers1

3

You did not provide the following constructor for your smallocator :

template <class U>
smallocator(const smallocator<U>& a) throw();

You need all three :

smallocator() throw();
smallocator(const smallocator& a) throw();
template <class U>
smallocator(const smallocator<U>& a) throw();
Sander De Dycker
  • 16,053
  • 1
  • 35
  • 40
  • @user2358643 : because the `std::vector` isn't using that constructor - it has a simpler (contiguous) memory layout than `std::deque` and can get by with just the default `smallocator()` constructor. – Sander De Dycker Jun 19 '13 at 14:02