自定义分配器适用于向量但不适用于双端队列(包装在队列中) [英] Custom allocator works with vector but not with deque (wrapped within a queue)

查看:21
本文介绍了自定义分配器适用于向量但不适用于双端队列(包装在队列中)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个自定义分配器,用于在共享内存段中分配内存.

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*> > ;

但是这行代码:

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

smallocator 的界面如下所示:

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!

推荐答案

您没有为您的 smallocator 提供以下构造函数:

You did not provide the following constructor for your smallocator :

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

您需要所有三个:

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

这篇关于自定义分配器适用于向量但不适用于双端队列(包装在队列中)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆